我正在尝试集成一个Web服务,其中JSON响应返回下一个1,000结果的URL。我要做的是循环cURL以继续处理请求,直到" next"变量是空的。
我遇到了一个问题,因为我看到变量返回后无法循环回来。
$url = "https://xxxxxxxxxxxxxx.com/process/api/transactions/from/$yesterday/to/$today/for/company/21?offset=0&limit=1000";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: $cookie"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
file_put_contents(rj_array, $result, FILE_APPEND);
$server = file_get_contents(rj_array);
$json_result = json_decode($server, true);
$next = $json_result['next'];
我在想做类似的事情:
while(!empty($next)){
//Do the curl request again with the new URL $next
}
但我每次都无法重置$ next变量。有没有人有任何想法?
答案 0 :(得分:2)
为什么不
$base_url = "https://xxxxxxxxxxxxxx.com/process/api/transactions/from/$yesterday/to/$today/for/company/21?limit=1000";
$next = 0;
while( $next !== false ) {
$url = $base_url . "&offset=" . $next;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: $cookie"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
file_put_contents(rj_array, $result, FILE_APPEND);
$server = file_get_contents(rj_array);
$json_result = json_decode($server, true);
$next = $json_result['next'];
if( /*test if next is empty*/ ) {
$next = false;
}
}
答案 1 :(得分:1)
我喜欢递归函数方法:
function crawler($url, $return) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: $cookie"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
file_put_contents(rj_array, $result, FILE_APPEND);
$server = file_get_contents(rj_array);
$json_result = json_decode($server, true);
$return[$url] = $json_result; //set your return information in some array
$next = $json_result['next'];
if(!empty($next)) {
return crawler($next, $return); //call again same function with next url and array of data that you would use later
}
return $return;
}
$url = "https://xxxxxxxxxxxxxx.com/process/api/transactions/from/$yesterday/to/$today/for/company/21?offset=0&limit=1000";
$allJsonResults = crawler($url, []);