Getting full list of contacts from Hubspot API

2.5k views Asked by At

Hubspot's API allows you retrieve a list of contacts, however it only allows a max of 100 per call.

I do that with this call:

$contacts_batch1 = $contacts->get_all_contacts(array( 'count' => '100'));

And then if I want to get the next 100 I do this:

$offset1 = $contacts_batch1->{'vid-offset'};

$contacts_batch2 = $contacts->get_all_contacts(array('count' => '100', 'vidOffset'=>$offset1));

I am trying to get all the contacts without having to create a new variable each time I want the next 100. My first question would be how would I go about getting the vid-offset of the last set, and then how would I put that as a parameter into the next variable automatically.

1

There are 1 answers

0
Ryan Jones On BEST ANSWER

Here's an example of getting all contacts into one array using HubSpot's API.

<?php
require "haPiHP/class.contacts.php";
require "haPiHP/class.exception.php";
define("HUBSPOT_API_KEY", "<YOUR API KEY HERE>");

$contacts = new HubSpot_Contacts(HUBSPOT_API_KEY);

$all_contacts = array();

do 
{
    $params = array("count" => 100);
    if (isset($vidOffset))
    {
        $params["vidOffset"] = $vidOffset;
    }

    echo "count=" . $params["count"] . (isset($params["vidOffset"]) ? ", vidOffset=" . $params["vidOffset"] : "") . "\n";

    $some_contacts = $contacts->get_all_contacts($params);
    if ($some_contacts !== NULL)
    {
        $all_contacts = array_merge($all_contacts, $some_contacts->contacts);
    }
    else
    {
        break;
    }

    $vidOffset = $some_contacts->{'vid-offset'};

} while ($some_contacts->{'has-more'});

echo "Received " . count($all_contacts) . " contacts.\n";

?>