Current Path :
/
home
/
zrcdevco
/
public_html
/
medrio
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/zrcdevco/public_html/medrio/get_hubspot_contact_lists.php
<?php echo("<h1>Test Getting HubSpot Contact Lists</h1>"); require 'vendor/autoload.php'; use HubSpot\Factory; use HubSpot\Client\Crm\Contacts\ApiException; use HubSpot\Client\Crm\Contacts\Model\Filter; use HubSpot\Client\Crm\Contacts\Model\FilterGroup; use HubSpot\Client\Crm\Contacts\Model\PublicObjectSearchRequest; use HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput; /************** * * Production * We need both the new API client created as well as * the Authentication header for using the older API. * **************/ // Production Authentication Header $authorization = "Authorization: Bearer pat-na1-4d0456ec-772d-4a2a-b16b-3f9a5fe3b749"; // TEST Authentication Header //$authorization = "Authorization: Bearer pat-na1-e33479fd-da09-4d3f-b7ba-883334bb0751"; // Setup Filters to check if contact in database? $filter1 = new Filter([ 'value' => '', 'property_name' => 'email', 'operator' => 'EQ' ]); $filterGroup1 = new FilterGroup([ 'filters' => [$filter1] ]); $publicObjectSearchRequest = new PublicObjectSearchRequest([ 'filter_groups' => [$filterGroup1], 'properties' => [ 'clinical_leader___content_titles_engaged_with', 'clinical_leader___content_topics_engaged_with', 'clinicalleaderdownloads__c'] ]); // Create a List and add the Contacts from this Upload to the list echo "<h3>Contact List</h3>"; // get date for HubSpot list name date $processDate = date("Y-m-d",strtotime('today')); $listDate = str_replace('-','',$processDate); $listName = $listDate.'-upload-staticlistb'; // Check if list exists // setup a filter $listFilterJson='"filters": [ [ { "filterFamily": "PropertyValue", "withinTimeMode": "PAST", "checkPastVersions": false, "type": "string", "property": "name", "value": $listName, "operator": "IS_EQUAL_TO" } ] ]'; $listExists=false; $hasMore=true; $curlOffset=0; $curlCount=250; while (!$listExists and $hasMore) { // create & initialize a curl session to retrieve a new list $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization)); // set our url with curl_setopt() // include the count and offset (when available) // to process 250 or more lists curl_setopt($ch, CURLOPT_URL, "https://api.hubapi.com/contacts/v1/lists?count=".$curlCount."&offset=".$curlOffset); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $post=json_encode(array('dynamic'=>'false')); curl_setopt($ch, CURLOPT_POSTFIELDS,$post); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // convert to an associative array to handle the // "has-more" value in the output // which is illegal as an object/variable name $result = json_decode(curl_exec($ch),true); curl_close($ch); //var_dump($result); $lists=$result["lists"]; //var_dump($lists); foreach($lists as $list){ //var_dump($list); if ($list["name"] == $listName) { $listExists=true; echo("<p>List ".$listName." exists - new contacts will be added</p>"); $listId=$list["listId"]; echo "List ID is = ".$listId."</br>"; } } $hasMore=$result["has-more"]; echo "Has More ".$hasMore."</br>"; $curlOffset=$result["offset"]; echo "Offset ".$curlOffset."</br>"; } if (!$listExists) { // create & initialize a curl session to retrieve a new list $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization)); // set our url with curl_setopt() curl_setopt($ch, CURLOPT_URL, "https://api.hubapi.com/contacts/v1/lists"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $post=json_encode(array('name' => $listName,'dynamic'=>'false')); curl_setopt($ch, CURLOPT_POSTFIELDS,$post); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $result = json_decode(curl_exec($ch)); curl_close($ch); //var_dump($result); $listId=$result->listId; echo "List Name is = ".$listName."- new list</br>"; echo "List ID is = ".$listId."</br>"; }