Skip to content

Instantly share code, notes, and snippets.

@adrianmott
Created February 6, 2012 19:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adrianmott/1754163 to your computer and use it in GitHub Desktop.
Save adrianmott/1754163 to your computer and use it in GitHub Desktop.
HubSpot to SugarCRM Integration Code Sample
<?php
$hubspot_config = array (
'hsportalid' => '######',
'hapikey' => 'XXXXXX',
'hapiuser' => 'XXXXXX',
'hapipassword' => 'XXXXXX',
'sugarinstallpath' => 'http://somesite.com/sugarcrm/',
'hapilogfile' => './sycnlog.txt');
?>
<?php
// hubspot_newleads.php
// require the configuration file
require 'hubspot_config.php';
// Log startup
$starttimestamp = date("Y-m-d H:i:s", time());
$runmsg = $starttimestamp . "--------Processing New HubSpot Lead-------------";
file_put_contents($hubspot_config['hsapilogfile'], $runmsg . "\r\n", FILE_APPEND);
// Let SugarCRM know that this is a valid file
define('sugarEntry', TRUE);
//Use the NuSOAP files included with SugarCRM to authenticate against the SOAP APO
//Setup NuSOAP session
require_once('../include/nusoap/nusoap.php');
$soapclient = new nusoapclient( $hubspot_config['sugarinstallpath'] . 'soap.php?wsdl',true);
$user_auth = array(
'user_auth' => array(
'user_name' => $hubspot_config['hsapiuser'],
'password' => md5($hubspot_config['hsapipassword']),
'version' => '0.1'),
'application_name' => 'soapleadcapture');
$result_array = $soapclient->call('login',$user_auth);
$session_id = $result_array['id'];
$user_guid = $soapclient->call('get_user_id',$session_id);
// liststarttime should be updated to run beginning with the end time for the last run. In this example, I'm just setting it to April 1 2010 for demonstration purposes.
$liststarttime = 1272722847000; //(Pull leads since April 1 2010)
//build HubSpot API urls
$listurl = 'https://hubapi.com/leads/list/' . $hubspot_config['hsportalid'] . '/?hapikey=' . $hubspot_config['hsapikey'] . '&startTime=' . $liststarttime;
$leadurl = 'https://hubapi.com/leads/lead/';
$leadurlsuffix = '?portalId=' . $hubspot_config['hsportalid'] . '&hapikey=' . $hubspot_config['hsapikey'];
// Fetch Lead List from HubSpot via curl
$chlist = curl_init();
curl_setopt($chlist, CURLOPT_URL, $listurl);
curl_setopt($chlist, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $chlist, CURLOPT_SSL_VERIFYPEER, false );
$output = curl_exec($chlist);
$apierr = curl_errno($chlist);
$apierrmsg = curl_error($chlist);
curl_close($chlist);
//if no error was received from the HubSpot API, process the results
if ($apierr == 0)
{
//decode JSON output
$leadlist = json_decode($output);
//loop through lead list and add new leads to SugarCRM
foreach ($leadlist as $lead)
{
$addlead = 0;
//try to get lead by HubSpot guid by checking custom field on existing leads
$get_entry_params = array('session' => $session_id,
'module_name' => 'Leads',
'select_fields'=>array( 'id', 'hubspot_guid_c' ),
'query' => "leads.id = (select leads_cstm.id_c from leads_cstm where leads_cstm.hubspot_guid_c = '" . $lead->guid . "')");
$guidresult = $soapclient->call('get_entry_list',$get_entry_params);
// if lead is not found by guid, search for lead by email by checking Leads, Accounts, Opportunities, and Contacts objects in SugarCRM
if ($guidresult['result_count'] == 0)
{
$search_by_module_params = array('user_name' => $hubspot_config['hsapiuser'],
'password' => $hubspot_config['hsapipassword'],
'search_string' => $lead->email,
'modules' => array('Leads', 'Accounts','Opportunities','Contacts'));
$emailresult = $soapclient->call('search_by_module',$search_by_module_params);
$emailresultcount = 0;
// if lead is found by email, write to log file
if ($emailresult['result_count'] <> 0)
{
while ($emailresultcount < $emailresult['result_count'])
{
$searchmsg = "Lead Found by email: email=" . $lead->email . " SugarCRM Module=" . $emailresult['entry_list'][$emailresultcount]['module_name'] . " SugarCRM id=" . $emailresult['entry_list'][$emailresultcount]['id'];
file_put_contents($hubspot_config['hsapilogfile'], $searchmsg . "\r\n", FILE_APPEND);
$emailresultcount++;
}
}
// lead was not found by email or guid, set addlead indicator to 1
else
{
$addlead = 1;
$searchmsg = "Lead Not Found, adding new Lead: guid=" . $lead->guid . " email=" . $lead->email;
file_put_contents($hubspot_config['hsapilogfile'], $searchmsg . "\r\n", FILE_APPEND);
}
}
// if lead was found by guid, write to log file
else
{
$searchmsg = "Lead Found by guid: guid=" . $lead->guid . " SugarCRM id=" . $guidresult['entry_list'][0]['id'];
file_put_contents($hubspot_config['hsapilogfile'], $searchmsg . "\r\n", FILE_APPEND);
}
//add lead to SugarCRM if lead is not found
if ($addlead == 1)
{
$firstvisit = date('Y-m-d', substr($lead->firstVisitSetAt, 0, 10)); //hubspot_first_visit_c
$firstconversiondate = date('Y-m-d', substr($lead->insertedAt, 0, 10)); //hubspot_first_conversion_date_c
$recentconversiondate = date('Y-m-d', substr($lead->lastConvertedAt, 0, 10)); //hubspot_recent_conversion_date_c
$leadscore = $lead->score; //hubspot_lead_score_c
$twitterlink = 'http://twitter.com/' . $lead->twitterHandle; //hubspot_twitter_link_c
$numberconversion = count($lead->leadConversionEvents); //hubspot_number_conversion_c
$firstconversionevent = $lead->leadConversionEvents[0]->formName; //hubspot_first_conversion_event_c
$recentconversionevent = $lead->leadConversionEvents[$numberconversion - 1]->formName; //hubspot_recent_cv_event_c
$foundvia = $lead->foundVia; //hubspot_found_site_via_c
$leadintellink = $lead->publicLeadLink; //hubspot_lead_intel_c
$set_entry_params = array(
'session' => $session_id,
'module_name' => 'Leads',
'name_value_list'=>array(
array('name'=>'first_name','value'=>$lead->firstName),
array('name'=>'last_name','value'=>$lead->lastName),
array('name'=>'email1','value'=>$lead->email),
array('name'=>'account_name','value'=>$lead->company),
array('name'=>'title','value'=>$lead->jobTitle),
array('name'=>'phone_work', 'value'=>$lead->phone),
array('name'=>'phone_fax', 'value'=>$lead->fax),
array('name'=>'primary_address_street','value'=>$lead->address),
array('name'=>'primary_address_city','value'=>$lead->city),
array('name'=>'primary_address_state','value'=>$lead->state),
array('name'=>'primary_address_postalcode','value'=>$lead->zip),
array('name'=>'primary_address_country','value'=>$lead->country),
array('name'=>'hubspot_guid_c', 'value'=>$lead->guid),
array('name'=>'hubspot_first_visit_c','value'=>$firstvisit),
array('name'=>'hubspot_first_conversion_date_c','value'=>$firstconversiondate),
array('name'=>'hubspot_recent_conversion_date_c','value'=>$recentconversiondate),
array('name'=>'hubspot_lead_score_c','value'=>$leadscore),
array('name'=>'hubspot_twitter_link_c', 'value'=>$twitterlink),
array('name'=>'hubspot_number_conversion_c', 'value'=>$numberconversion),
array('name'=>'hubspot_first_conversion_event_c','value'=>$firstconversionevent),
array('name'=>'hubspot_recent_cv_event_c','value'=>$recentconversionevent),
array('name'=>'hubspot_found_site_via_c','value'=>$foundvia),
array('name'=>'hubspot_lead_intel_c','value'=>$leadintellink),
array('name'=>'lead_source','value'=>'Web Site'),
array('name'=>'status', 'value'=>'New'),
array('name'=>'assigned_user_id', 'value'=>$user_guid)));
$addleadresult = $soapclient->call('set_entry',$set_entry_params);
if ($addleadresult['error']['number'] == 0)
{
$addmsg = "-|-Lead Created: SugarCRM id=" . $addleadresult['id'];
file_put_contents($hubspot_config['hsapilogfile'], $addmsg . "\r\n", FILE_APPEND);
}
else
{
$addmsg = "---Error Adding Lead: Error Message=" . $addleadresult['number'] . "-" . $addleadresult['name'] . "-" . $addleadresult['description'];
file_put_contents($hubspot_config['hsapilogfile'], $addmsg . "\r\n", FILE_APPEND);
}
}
}
}
//if HubSpot API returned an error, log error
else
{
$errmsg = "HubSpot API Call Unsuccessful: " . $apierr . " - " . $apierrmsg;
file_put_contents($hubspot_config['hsapilogfile'], $errmsg . "\r\n", FILE_APPEND);
}
//Log shutdown
$endtimestamp = date("Y-m-d H:i:s", time());
$runmsg = $endtimestamp . "--------Processing Complete-------------";
file_put_contents($hubspot_config['hsapilogfile'], $runmsg . "\r\n", FILE_APPEND);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment