【问题标题】:Hubspot API: Getting all contacts (100+)Hubspot API:获取所有联系人 (100+)
【发布时间】:2018-11-13 10:45:07
【问题描述】:

我正在尝试创建一个包含所有联系人的大型 json 文件。由于 API 返回最多 100 个联系人,因此我必须使用分页。

我在此找到了older topic,但这使用了外部repo。我找到了一个newer repo,但无法让它工作。最好的情况是,我不使用外部库,因为我需要这么小的一段代码。

我尝试了以下代码,但它一直在加载。我的猜测是变量不会更新。我做错了什么?

<?php


echo '<pre>';
function getData($offset = 0){

$properties = "&property=email&property=firstname&property=funnel&property=hs_lead_status&property=start_session";
$apikey = "xxxx-xx-xx";
$feed_url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/recent?hapikey=". $apikey.  $properties.'&vidOffset='.$offset;

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $feed_url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache"
  ),
));
$response = curl_exec($curl);
$data = json_decode($response);

return $data; //return the results for use
}

$allData = array();




$offset = 0;
$hasMore = true;

while ($hasMore === true) {

    $response = getData( $offset );
    $allData[] = $response;


    $offset = $response->{'vid-offset'};
    $hasMore = $response->{'has-more'};

    var_dump( $hasMore );
    $hasMore = false;
}

【问题讨论】:

    标签: api loops while-loop hubspot


    【解决方案1】:

    我能想到的一件事是,您的循环没有考虑到 HubSpot 每秒只允许 10 个请求的事实(请参阅here) - 您的循环可能超过了这一点,因此导致了问题。您应该在 while 循环结束之前添加以下代码(这将使脚本休眠 0.1 秒):

    usleep(100000);
    

    此外,使用 PHP API 库(例如 this one)可能是一个更好、更简洁的主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 2018-11-11
      相关资源
      最近更新 更多