Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick and Dirty Hubspot Property Export Script
<?php
define("HUBSPOT_API_KEY", "YOUR_API_KEY");
$filename = 'hs-property-export.csv';
$objectTypes = [
'Contact',
'Company',
'Deal',
'Ticket'
];
$csvData = [];
foreach ($objectTypes AS $objectType) {
$curl = curl_init();
$archived = false;
$curlOpt = [
CURLOPT_URL => "https://api.hubapi.com/crm/v3/properties/" . $objectType . "?archived=" . $archived . "&hapikey=" . HUBSPOT_API_KEY,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"accept: application/json"
]
];
curl_setopt_array($curl, $curlOpt);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
die("cURL Error #:" . $err);
}
$response = json_decode($response, true);
$csvDataColumns = [
'groupName',
'name',
'label',
'type',
'fieldType'
];
$optionDataColumns = [
'label',
'value',
'description',
'hidden',
'label'
];
foreach ($response['results'] AS $idx => $result) {
$row = [
'is_option' => 0,
'has_options' => (array_key_exists('options', $result) ? 1 : 0),
'object' => $objectType
];
foreach ($csvDataColumns AS $csvDataColumn) {
if (array_key_exists($csvDataColumn, $result)) {
$row[$csvDataColumn] = $result[$csvDataColumn];
} else {
$row[$csvDataColumn] = '';
}
}
foreach ($optionDataColumns AS $optionDataColumn) {
if (array_key_exists($csvDataColumn, $result)) {
$row['option_' . $optionDataColumn] = '';
}
}
$csvData[] = $row;
if (array_key_exists('options', $result)) {
foreach ($result['options'] AS $option) {
$row = [
'is_option' => 1,
'has_options' => '',
'object' => $objectType
];
foreach ($csvDataColumns AS $csvDataColumn) {
if ($csvDataColumn == 'name' || $csvDataColumn == 'groupName') {
$row[$csvDataColumn] = $result[$csvDataColumn];
} else {
$row[$csvDataColumn] = '';
}
}
$row['has_options'] = '';
$row['is_option'] = 1;
foreach ($optionDataColumns AS $optionDataColumn) {
if (array_key_exists($optionDataColumn, $option)) {
$row['option_' . $optionDataColumn] = $option[$optionDataColumn];
} else {
$row['option_' . $optionDataColumn] = '';
}
}
$csvData[] = $row;
}
}
}
}
$fp = fopen("php://memory", 'w');
$headers = array_keys($csvData[0]);
array_unshift($csvData, $headers);
foreach ($csvData AS $row) {
fputcsv($fp, array_values($row));
}
rewind($fp);
$csv = stream_get_contents($fp);
fclose($fp);
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '.csv";');
header('Content-Transfer-Encoding: binary');
exit($csv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.