Created
August 23, 2022 02:09
-
-
Save HubSpotHanevold/a7d733edd1303fc9fb50c6a51963b59b to your computer and use it in GitHub Desktop.
A way to output HubSpot custom objects to a CSV file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// UPDATE THESE SETTINGS BELOW | |
// ENTER THE CUSTOM OBJECT ID HERE | |
$custom_object_id = 'XXXXXXXXXX'; | |
// ENTER ALL THE PROPERTIES YOU WANT TO OUTPUT TO CSV - NOTE IT MUST MATCH YOUR INTERNAL NAME FROM HUBSPOT | |
$properties_array = array('XXXXXXXXXXX', 'XXXXXXXXXXXXXXXX'); | |
// NOTE THAT THINGS MIGHT BREAK IF THE PROPERTY IS A SINGLE LINE TEXT THAT CAN CONTAIN A COMMA | |
// USE YOUR PRIVATE APP TOKEN BELOW | |
$hapi_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; | |
// DONT UPDATE BELOW THIS COMMENT LINE | |
// DONT UPDATE BELOW THIS COMMENT LINE | |
// DONT UPDATE BELOW THIS COMMENT LINE | |
$count_properties = count($properties_array); | |
ob_start(); | |
$x = 0; | |
while($x < $count_properties) { | |
if(($x + 1)== $count_properties) { | |
$ampersand = ''; | |
} else { | |
$ampersand = '&'; | |
} | |
echo 'properties='.$properties_array[$x].$ampersand; | |
$x++; | |
} | |
$all_properties = ob_get_contents(); | |
ob_end_clean(); | |
ob_end_flush(); | |
// START THE OUTPUT OF CUSTOM OBJECT DATA TO CSV | |
ob_start(); | |
$p = 0; | |
while($p < $count_properties) { | |
if(($p + 1) == $count_properties) { | |
$terminating_comma_header = ''; | |
} else { | |
$terminating_comma_header = ','; | |
} | |
echo $properties_array[$p].$terminating_comma_header; | |
$p++; | |
} | |
echo "\n"; | |
$a = 0; | |
do { | |
// RETRIEVE ALL THE RECORDS WITH A LOOP | |
if($a == 0) { | |
$request_url = "https://api.hubapi.com/crm/v3/objects/$custom_object_id?limit=100&$all_properties&archived=false"; | |
} else { | |
$request_url = $new_request_url; | |
} | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $request_url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => '', | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => 'GET', | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer $hapi_key" | |
), | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
$data = json_decode($response, true); | |
$results = $data['results']; | |
$results_count = count($results); | |
$y = 0; | |
while($y < $results_count) { | |
$z = 0; | |
while($z < $count_properties) { | |
if(($z + 1) == $count_properties) { | |
$terminating_comma = ''; | |
} else { | |
$terminating_comma = ','; | |
} | |
echo $data['results'][$y]['properties'][$properties_array[$z]].$terminating_comma; | |
$z++; | |
} | |
echo "\n"; | |
$y++; | |
} | |
$new_request_url = $data['paging']['next']['link']; | |
if(empty($new_request_url)) { | |
$exit = 'true'; | |
} else { | |
$exit = 'false'; | |
} | |
$a++; | |
} while($exit == 'true'); | |
$request_output = ob_get_contents(); | |
ob_end_clean(); | |
ob_end_flush(); | |
// WRITE THE CSV FILE | |
$file = fopen('custom_object_data.csv', 'w'); | |
fwrite($file, $request_output); | |
fclose($file); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment