note 110620 added to function.curl-multi-exec

From: Date: Wed, 14 Nov 2012 03:52:24 +0000
Subject: note 110620 added to function.curl-multi-exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-191920@lists.php.net to get a copy of this message
There is no limit on the number of concurrent connections opened by curl. You need to add this kind of functionality yourself. But luckily a genius called Josh Fraser already did all the hard work for us : http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/ All credits go to Josh! The $rolling_window property is equal to the maximum amount of concurrent connections. For archiving purposes I have copied the method here, just in case his website goes missing. Refer to the link above for the latest version. function rolling_curl($urls, $callback, $custom_options = null) { // make sure the rolling window isn't greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window; $master = curl_multi_init(); $curl_arr = array(); // add additional curl options here $std_options = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5); $options = ($custom_options) ? ($std_options + $custom_options) : $std_options; // start the first batch of requests for ($i = 0; $i < $rolling_window; $i++) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i]; curl_setopt_array($ch,$options); curl_multi_add_handle($master, $ch); } do { while(($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); if($execrun != CURLM_OK) break; // a request was just completed -- find out which one while($done = curl_multi_info_read($master)) { $info = curl_getinfo($done['handle']); if ($info['http_code'] == 200) { $output = curl_multi_getcontent($done['handle']); // request successful. process output using the callback function. $callback($output); // start a new request (it's important to do this before removing the old one) $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i++]; // increment i curl_setopt_array($ch,$options); curl_multi_add_handle($master, $ch); // remove the curl handle that just completed curl_multi_remove_handle($master, $done['handle']); } else { // request failed. add error handling. } } } while ($running); curl_multi_close($master); return true; } ---- Server IP: 69.147.83.199 Probable Submitter: 202.32.29.250 ---- Manual Page -- http://www.php.net/manual/en/function.curl-multi-exec.php Edit -- https://master.php.net/note/edit/110620 Del: integrated -- https://master.php.net/note/delete/110620/integrated Del: useless -- https://master.php.net/note/delete/110620/useless Del: bad code -- https://master.php.net/note/delete/110620/bad+code Del: spam -- https://master.php.net/note/delete/110620/spam Del: non-english -- https://master.php.net/note/delete/110620/non-english Del: in docs -- https://master.php.net/note/delete/110620/in+docs Del: other reasons-- https://master.php.net/note/delete/110620 Reject -- https://master.php.net/note/reject/110620 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191920) next »