note 124240 added to function.curl-multi-exec

From: Date: Fri, 27 Sep 2019 18:53:12 +0000
Subject: note 124240 added to function.curl-multi-exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-212364@lists.php.net to get a copy of this message
Just for people struggling to get this to work, here is my approach. No infinite loops, no CPU 100%, speed can be tweaked. <?php function curl_multi_exec_full($mh, &$still_running) { do { $state = curl_multi_exec($mh, $still_running); } while ($still_running > 0 && $state === CURLM_CALL_MULTI_PERFORM && curl_multi_select($mh, 0.1)); return $state; } function curl_multi_wait($mh, $minTime = 0.001, $maxTime = 1){ $umin = $minTime*1000000; $start_time = microtime(true); $num_descriptors = curl_multi_select($mh, $maxTime); if($num_descriptors === -1){ usleep($umin); } $timespan = (microtime(true) - $start_time); if($timespan < $umin){ usleep($umin - $timespan); } } $handles = [ [ CURLOPT_URL=>"http://example.com/", CURLOPT_HEADER=>false, CURLOPT_RETURNTRANSFER=>true, CURLOPT_FOLLOWLOCATION=>false, ], [ CURLOPT_URL=>"http://www.php.net", CURLOPT_HEADER=>false, CURLOPT_RETURNTRANSFER=>true, CURLOPT_FOLLOWLOCATION=>false, // https://stackoverflow.com/a/41135574 CURLOPT_HEADERFUNCTION=>function($ch, $header) { print "header from http://www.php.net: ".$header; return strlen($header); } ] ]; $mh = curl_multi_init(); $chandles = []; foreach($handles as $opts) { $ch = curl_init(); curl_setopt_array($ch, $opts); curl_multi_add_handle($mh, $ch); $chandles[] = $ch; } $prevRunning = null; do { $status = curl_multi_exec_full($mh, $running); if($running < $prevRunning){ while ($read = curl_multi_info_read($mh, $msgs_in_queue)) { $info = curl_getinfo($read['handle']); if($read['result'] !== CURLE_OK){ print "Error: ".$info['url'].PHP_EOL; } if($read['result'] === CURLE_OK){ /* if(isset($info['redirect_url']) && trim($info['redirect_url'])!==''){ print "running redirect: ".$info['redirect_url'].PHP_EOL; $ch3 = curl_init(); curl_setopt($ch3, CURLOPT_URL, $info['redirect_url']); curl_setopt($ch3, CURLOPT_HEADER, 0); curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, 0); curl_multi_add_handle($mh,$ch3); } */ print_r($info); //echo curl_multi_getcontent($read['handle'])); } } } if ($running > 0) { curl_multi_wait($mh); } $prevRunning = $running; } while ($running > 0 && $status == CURLM_OK); foreach($chandles as $ch){ curl_multi_remove_handle($mh, $ch); } curl_multi_close($mh); ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.168 (proxied: 81.38.199.121) ---- Manual Page -- http://php.net/manual/en/function.curl-multi-exec.php Edit -- https://master.php.net/note/edit/124240 Del: integrated -- https://master.php.net/note/delete/124240/integrated Del: useless -- https://master.php.net/note/delete/124240/useless Del: bad code -- https://master.php.net/note/delete/124240/bad+code Del: spam -- https://master.php.net/note/delete/124240/spam Del: non-english -- https://master.php.net/note/delete/124240/non-english Del: in docs -- https://master.php.net/note/delete/124240/in+docs Del: other reasons-- https://master.php.net/note/delete/124240 Reject -- https://master.php.net/note/reject/124240 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#212364) next »