Created
February 10, 2017 14:18
-
-
Save Sommerregen/bd69b542c7f85de7f50e83d10a420550 to your computer and use it in GitHub Desktop.
Asynchronous multi Curl request without blocking and rolling window (see https://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getHead($urls){ | |
$results = array(); | |
// 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 | |
$options = array( | |
CURLOPT_FOLLOWLOCATION => FALSE, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_NOBODY => TRUE, | |
); | |
// start the first batch of requests | |
for ($i = 0; $i < $rolling_window; $i++) { | |
$ch = curl_init(); | |
$options[CURLOPT_URL] = array_pop($urls); | |
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']); | |
$results[$info['url']] = $info; | |
$new_url = array_pop($urls); | |
if(isset($new_url)){ | |
$ch = curl_init(); | |
$options[CURLOPT_URL] = $new_url; | |
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']); | |
} | |
} while ($running); | |
curl_multi_close($master); | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment