Skip to content

Instantly share code, notes, and snippets.

@adrianmott
Created February 6, 2012 19:54
Show Gist options
  • Save adrianmott/1754406 to your computer and use it in GitHub Desktop.
Save adrianmott/1754406 to your computer and use it in GitHub Desktop.
HubSpot to Highrise CRM Integration Code Sample
<?php
//First, we start by opening the log file and getting the last time this file ran.
$logfile = "hubspotLeadTime.log"; //Make sure you have the correct permissions on these log files on your server.
$leadlog = "hubspotLeadLog.log";
$apikey = "XXXXXXXXXX"; //replace this with your own key
if (file_exists($logfile)) { //Proceed like normal
$fileh = fopen($logfile, 'r') or die("can't open file");
$oldtime = fread($fileh, filesize($logfile));
fclose($fileh);
//We're then going to Log the current time, then place it into our log file, overwriting the old time to prevent the log file growing huge...
$newtime = time();
$passedtime = $newtime*1000;
$fileh = fopen($logfile, 'w') or die("can't open file");
fwrite($fileh, $passedtime);
fclose($fileh);
}
else { //Create the file and place the first timestamp in there.
$newtime = time();
$passedtime = $newtime*1000;
$fileh = fopen($logfile, 'w') or die("can't open file");
fwrite($fileh, "".$passedtime."\n");
fclose($fileh);
}
//GET request to get leads form HubSpot SINCE the $oldtime in the log file.
$jsonurl = 'https://hubapi.com/leads/v1/list/' . '?hapikey=' . $apikey . '&startTime=' . $oldtime; //replace the hapikey key with your own here!
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json,true);
foreach ($json_output as $lead) { //cycle through the array (lead points to the array...)
//Search Hughrise for leads with email matching lead's email from HubSpot. Since we can't properly store GUID in Highrise, we'll only de-dup by email
$cl = curl_init();
curl_setopt($cl, CURLOPT_URL, "http://<YOUR COMPANY>.highrisehq.com/people/search.xml?criteria[email]=$lead[email]");
curl_setopt($cl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cl, CURLOPT_USERPWD, "<Your Highrise API Token>:X");
$result = curl_exec($cl);
curl_close($cl);
if (preg_match("/nil-classes/i", $result)) {
//"nil-classes" is part of the XML that gets returned in the empty person record by Highrise.
//process the lead and pass it into Highrise...
//This will build the XML string that gets passed into. Note the 'guid' in the background field.
$xml = "
<person>
<first-name>$lead[firstName]</first-name>
<last-name>$lead[lastName]</last-name>
<title>$lead[jobTitle]</title>
<background>Found Site Via: $lead[fullFoundViaString]\n
First Visit: $convertDate\n
HubSpot Lead Grade: $lead[score]\n
Lead in HubSpot: $lead[publicLeadLink]\n
Number of Conversion Events: $lead[numConversionEvents]\n
UserToken: $lead[userToken]\n
IP Address: $lead[ipAddress]\n
HubSpot Guid: $lead[guid]</background>
<visible-to>Everyone</visible-to>
<contact-data>
<email-addresses>
<email-address>
<address>$lead[email]</address>
<location>Work</location>
</email-address>
</email-addresses>
<phone-numbers>
<phone-number>
<number>$lead[phone]</number>
<location>Work</location>
</phone-number>
</phone-numbers>
<addresses>
<address>
<city>$lead[city]</city>
<country>$lead[country]</country>
<state>$lead[state]</state>
<street>$lead[address]</street>
<zip>$lead[zip]</zip>
<location>Work</location>
</address>
</addresses>
<web-addresses>
<web-address>
<url>$lead[website]</url>
<location>Work</location>
</web-address>
</web-addresses></contact-data>
</person>";
//cURL this XML into Highrise to create the new person record.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://<YOUR COMPANY>.highrisehq.com/people.xml");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_USERPWD, "<Your Highrise API Token>:X");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
$http_code = curl_getinfo($curl ,CURLINFO_HTTP_CODE);
curl_close($curl);
//Write to the log file saying we created the new lead in Highrise.
if ($http_code == "201") {
//If lead creation succeeded, HTTP 201 is returned, so log successful lead creation.
$logleadlang = "".$http_code. " Created Lead with GUID: ".$lead[guid]."";
$logs = fopen($leadlog, 'a') or die("can't open file");
fwrite($logs, $logleadlang);
fclose($logs);
}
else {
//If lead creation failed, log the failure and the guid for the lead that didn't get entered.
$failedlang = "Lead creation failed for GUID: " .$lead[guid]."";
$logs = fopen($leadlog, 'a') or die("can't open file");
fwrite($logs, $logleadlang);
fclose($logs);
}
}
else {
break; //lead's email address was found, exit loop and stop...
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment