2015-06-14 8 views
2

Я работаю с несколькими Curl и задавался вопросом, как обращаться с ошибками. Я хочу проверить, какая ошибка произошла, и если это ошибка, как, превысил предел скорости. Я хочу снова просканировать эту ссылку после некоторой задержки (sleep()). Мой вопрос: «Есть ли встроенная функция, которая может сделать это для меня или мне нужно собрать все Urls в массиве и снова запустить их?»Multi Curl, обработка ошибок

Это то, что я получил в настоящее время:

<?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); 
?> 

ответ

0

Во втором для цикла, когда вы велосипеде через обработчики локон, чтобы исследовать то, что сделал каждый локон возвращение обработчика, я надеюсь, этот подход будет ответьте на ваш вопрос

foreach ($ch as $key => $h) { 

//This code is actually checking for any error that may occur, whatever that 
//error is you can handle it in the if-part of the condition. and save those 
//urls to the array $again to call them on a later stage. 

if (curl_errno($h)) { 

//this is how you will get complete information what did happened to the 
//curl handler. And why did it fail. All the inforation will be stored in //error_info. 

$again[] = array("Url" =>curl_getinfo($h, CURLINFO_EFFECTIVE_URL), "error_info" => curl_getinfo($h)); 

} 
else{ 

//here you will handle the success scenario for each curl handler. 

$responses[$key] = ['data' => curl_multi_getcontent($h)]; 
} 

//remove curl handler as you are doing in the loop 

} 
Смежные вопросы