2
我正在與多捲曲,並想知道如何處理錯誤。我想檢查發生了哪個錯誤,如果它是一個錯誤,超過了速率限制,我想在延遲一段時間後重新抓取該鏈接(sleep())。我的問題是:「是否有可以爲我做的這個函數的構建,還是我需要收集數組中的所有Url,然後再運行一次?」多捲曲,錯誤處理
這是我現在得到:
<?php
$urls = array( "https://API-URL.com",
"https://API-URL.com",
"https://API-URL.com",
"https://API-URL.com",
...);
//create the multiple cURL handle
$mh = curl_multi_init();
//Number of elements in $urls
$nbr = count($urls);
// set URL and options
for($x = 0; $x < $nbr; $x++){
// create both cURL resources
$ch[$x] = curl_init();
// set URL and other appropriate options
curl_setopt($ch[$x], CURLOPT_URL, $urls[$x]);
curl_setopt($ch[$x], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$x], CURLOPT_SSL_VERIFYPEER, false);
//add the two handles
curl_multi_add_handle($mh,$ch[$x]);
}
//execute the handles
do {
curl_multi_exec($mh, $running);
} while ($running);
for($x = 0; $x < $nbr; $x++){
$result = curl_multi_getcontent($ch[$x]);
$decoded = json_decode($result, true);
//get info about the request
$error = curl_getinfo($ch[$x], CURLINFO_HTTP_CODE);
//error handling
if($error != 200){
$again[] = array("Url" => $urls[$x], "errornbr" => $error);
} else {
// Here I do what ever I want with the data
}
curl_multi_remove_handle($mh, $ch[$x]);
curl_close($ch[1]);
}
curl_multi_close($mh);
?>