【发布时间】: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