【问题标题】:Get all the contacts from HubSpot list using API使用 API 从 HubSpot 列表中获取所有联系人
【发布时间】:2019-07-03 04:58:06
【问题描述】:

我正在使用联系人 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 获得所有联系,那么请分享您的经验。

【问题讨论】:

    标签: php wordpress api curl hubspot


    【解决方案1】:

    当您调用此 API 时,需要在此处查找一些内容。

    1. “拥有更多”字段中是否存在“真实”值。如果是这样,可以拉更多的联系人。

    2. 调用中返回的“vid-offset”字段的值。

    对于“拥有更多”,此布尔值指定是否有更多联系人可以通过分页拉取。对于“vid-offset”,这是一个API生成的整数,它不是一个简单的连续整数。

    此外,您一次只能抓取 5 条记录,您也可以只抓取最大值,因为它只有 100 条。这将限制您需要拨打的电话数量。

    最后,您可能只想将这些添加到文件中,然后您可以将其用于您想要的任何内容,即添加到数据库、下载等。

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

    documentation that they provide is actually quite clear,所以我也会仔细阅读它。

    【讨论】:

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