Skip to content

Instantly share code, notes, and snippets.

@reganstarr
Last active November 23, 2015 16:21
Show Gist options
  • Save reganstarr/0baa56840ec35df10c58 to your computer and use it in GitHub Desktop.
Save reganstarr/0baa56840ec35df10c58 to your computer and use it in GitHub Desktop.
<?php
if($_SERVER['REQUEST_METHOD'] != 'POST'){
exit;
}
$hubspotApiKey = "";
$listIdBefore100 = "";
$listIdAfter100 = "";
$workflowIdBefore100 = "";
$workflowIdAfter100 = "";
function enrollInWorkflow($workflowId, $emailAddress){
global $hubspotApiKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.hubapi.com/automation/v2/workflows/$workflowId/enrollments/contacts/$emailAddress?hapikey=$hubspotApiKey");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
function addContactToList($contactVid, $listId){
global $hubspotApiKey;
$array = array(
'vids' => array(
$contactVid //must be an integer, not a string
)
);
$json = json_encode($array);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.hubapi.com/contacts/v1/lists/$listId/add?hapikey=$hubspotApiKey");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
//receive post data
$json = $HTTP_RAW_POST_DATA;
$array = json_decode($json, true);
$contactVid = intval($array['vid']); //get the interger value
$contactEmail = $array['properties']['email']['value'];
//look up number of contacts in list
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.hubapi.com/contacts/v1/lists/$listIdBefore100?hapikey=$hubspotApiKey");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
$numberOfContactsInList = $response['metaData']['size'];
//enroll contact in "Workflow A" or "Workflow B"
if($numberOfContactsInList < 100){
addContactToList($contactVid, $listIdBefore100);
enrollInWorkflow($workflowIdBefore100, $contactEmail);
}
else{
addContactToList($contactVid, $listIdAfter100);
enrollInWorkflow($workflowIdAfter100, $contactEmail);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment