温馨提示:本文翻译自stackoverflow.com,查看原文请点击:php - Get all the contacts from HubSpot list using API
api php wordpress curl hubspot

php - 使用API​​从HubSpot列表中获取所有联系人

发布于 2020-03-27 11:53:30

我正在使用联系人API,但仅返回最多250个联系人。我在下一页使用了“ vidOffset”参数,但没有运气。

注意:我想使用API​​将所有联系人从Hubspot列表导出到我的本地数据库

这是我的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);

我做错什么了吗?或者,如果有更好的方法使用php / wordpress获得所有联系,请分享您的经验。

查看更多

查看更多

提问者
Manish Negi
被浏览
127
DubVader 2019-07-03 23:22

调用此API时,这里需要注意一些事项。

  1. “更多”字段中是否有“真”值。如果是这样,还有更多可以拉动的触点。

  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.

因此,我的建议是修改您的初始函数,以检查“ has-more”值是否为“ true”,如果为true,则将“ vid-offset”值发送给进行另一个调用的新函数。在该函数中,您可以继续检查这些值,并尽可能多地运行函数,直到该“ has-more”值变为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...

   }
}

他们提供文档实际上非常清晰,因此我也会对其进行仔细阅读。