我正在尝试用我的所有联系人创建一个大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;
}