Skip to content

Instantly share code, notes, and snippets.

Created August 26, 2015 17:03
Show Gist options
  • Save anonymous/1a9eb381f6a5f260bd20 to your computer and use it in GitHub Desktop.
Save anonymous/1a9eb381f6a5f260bd20 to your computer and use it in GitHub Desktop.
<?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;
}
@Fshamri
Copy link

Fshamri commented Sep 20, 2015

can you please provide an example of using your gits to download bulk images from internet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment