Skip to content

Instantly share code, notes, and snippets.

@lukecav
Forked from rothschild86/cf-hubspot.php
Created August 1, 2016 21:51
Show Gist options
  • Save lukecav/f2f403439579e283d0852aa4a6d9472e to your computer and use it in GitHub Desktop.
Save lukecav/f2f403439579e283d0852aa4a6d9472e to your computer and use it in GitHub Desktop.
Caldera Forms to HubSpot CRM integration
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.
//required plugins: code snippets, caldera forms, caldera forms run action
add_action('consult_req_submitted', 'consult_req_submitted_sync_hubspot');
function consult_req_submitted_sync_hubspot( $data ){
$hubspot_api_key = 'ffffffff-27a0-4591-9b68-27d4bb31ed76';
$hubspot_list_id = '1'; //optional
$caldera_name = $data[ 'your_name' ]; //this is the caldera field slug
$caldera_email = $data[ 'email' ]; //this is the caldera field slug
$caldera_phone = $data[ 'telephone' ]; //this is the caldera field slug //change caldera mask to 999-999-9999
$data = array('properties' => array());
$data['properties'][] = array('property' => 'firstname', 'value' => $caldera_name);
$data['properties'][] = array('property' => 'email', 'value' => $caldera_email);
$data['properties'][] = array('property' => 'phone', 'value' => $caldera_phone);
//$data['properties'][] = array('property' => 'lastname', 'value' => $caldera_lastname);
try {
$hubspot_url = 'https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/'.$caldera_email.'/?hapikey='.rawurlencode($hubspot_api_key);
$curl = curl_init($hubspot_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
//second call is optional
$hubspot_url = 'https://api.hubapi.com/contacts/v1/lists/'.$hubspot_list_id.'/add/?hapikey='.rawurlencode($hubspot_api_key);
$data = array('emails' => array($caldera_email));
$curl = curl_init($hubspot_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
} catch (Exception $e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment