Getting full list of contacts from Hubspot API

做~自己de王妃 提交于 2019-12-12 10:03:42

问题


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:


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";

?>


来源:https://stackoverflow.com/questions/30983587/getting-full-list-of-contacts-from-hubspot-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!