php - Multi Curl, error handling -
i'm working multi curl , wondering how handle errors. want check error occured , if error like, rate limit exceeded want crawl link again after delay (sleep()). question: "is there build in function can me or need collect urls in array , run again?"
this i've got now:
<?php $urls = array( "https://api-url.com", "https://api-url.com", "https://api-url.com", "https://api-url.com", ...); //create multiple curl handle $mh = curl_multi_init(); //number of elements in $urls $nbr = count($urls); // set url , options for($x = 0; $x < $nbr; $x++){ // create both curl resources $ch[$x] = curl_init(); // set url , 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 2 handles curl_multi_add_handle($mh,$ch[$x]); } //execute handles { 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 request $error = curl_getinfo($ch[$x], curlinfo_http_code); //error handling if($error != 200){ $again[] = array("url" => $urls[$x], "errornbr" => $error); } else { // here ever want data } curl_multi_remove_handle($mh, $ch[$x]); curl_close($ch[1]); } curl_multi_close($mh); ?>
in second for-loop, when cycling through curl handlers examine did each curl handler return, hope, approach answer question
foreach ($ch $key => $h) { //this code checking error may occur, whatever //error can handle in if-part of condition. , save //urls array $again call them on later stage. if (curl_errno($h)) { //this how complete information did happened //curl handler. , why did fail. inforation stored in //error_info. $again[] = array("url" =>curl_getinfo($h, curlinfo_effective_url), "error_info" => curl_getinfo($h)); } else{ //here handle success scenario each curl handler. $responses[$key] = ['data' => curl_multi_getcontent($h)]; } //remove curl handler doing in loop }
Comments
Post a Comment