Warm tip: This article is reproduced from stackoverflow.com, please click
api php wordpress curl hubspot

Get all the contacts from HubSpot list using API

发布于 2020-03-27 10:28:48

I am using the contact API but it returns maximum 250 contacts only. I used 'vidOffset' parameter for next page but no luck.

Note: I want to export all the contacts from the Hubspot List to my local database using API

Here is my code with php curl:

function callAPI($method, $url, $data){
   $curl = curl_init();
   $url = $url.'&property=firstname&property=email&count=5&vidOffset=2';
    switch ($method){
      case "POST":
         curl_setopt($curl, CURLOPT_POST, 1);
         if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         break;
      case "PUT":
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
         if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                              
         break;
      default:
         if ($data)
            $url = sprintf("%s?%s", $url, http_build_query($data));
   }
   // OPTIONS:
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
   ));
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

   // EXECUTE:
   $result = curl_exec($curl);
   if(!$result){die("Connection Failure");}
   curl_close($curl);
   return $result;
}

// call the function
callAPI('GET', 'https://api.hubapi.com/contacts/v1/lists/11/contacts/all?hapikey=[API key]', false);

Is there anything wrong I am doing? or if there is a better way to get all contact using php/wordpress then please share your experience.

Questioner
Manish Negi
Viewed
106
DubVader 2019-07-03 23:22

There are a few things to look for here when you make your calls to this API.

  1. Is there a "true" value in the "has-more" field. If so, there are more contacts that can be pulled.

  2. The value of the "vid-offset" field that is returned in your calls.

For the "has-more", this boolean specifies whether or not there are more contacts that you can pull via pagination. For the "vid-offset", this is an integer generated by the API, it does not take a simple sequential integer.

Additionally, you're only grabbing 5 records at a time, you may as well just do the maximum, since it's only 100. This will limit the number of calls you need to make.

Lastly, you may just want to add these to a file, which you can then use for whatever you want, i.e. adding to database, download etc.

So, my suggestion is to amend your initial function to check for the "has-more" value of "true", if it is true send the "vid-offset" value to a new function that makes another call. In that function, you can continue to check for those values, and run your function as many times as it takes until that "has-more" value turns up false.

   // the rest of your function is above

   // Decode the result so you can traverse the data

   $contacts = json_decode($result);

   // Store 'has-more' value

   $has_more = $contacts->has-more; 

   // Check if there are more records

   if($has_more) {

   // Get the offset number provided by API

   $offset = $contacts->vid-offset;

   // Get more records

   getMore($offset);

   } else { 

   // Complete calls and do something else...


   }

}

function getMore($offset) {

// Make cURL call with your your offset value

   $url = $url.'&property=firstname&property=email&count=100&vidOffset=' . $offset;

   $contacts = json_decode($result);

   $has_more = $contacts->has-more; 



   if($has_more) {

   $offset = $contacts->vid-offset;
   getMore($offset);

   } else {

    // Complete calls and do something else...

   }
}

The documentation that they provide is actually quite clear, so I would read it over a bit as well.