note 100007 deleted from function.curl-multi-exec by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 12:15:53 +0000 |
| Subject: | note 100007 deleted from function.curl-multi-exec by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197114@lists.php.net to get a copy of this message | ||
Note Submitter: zhongeren at gmail dot com
----
download all the images given in the URL array in parallel !!
<?php
/**
*
*@param $picsArr Array [0]=> [url],
*@$picsArr Array will filled with the image data ï¼ you can use the data as you want or just
save it in the next step.
**/
function getAllPics(&$picsArr){
if(count($picsArr)<=0) return false;
$hArr = array();//handle array
foreach($picsArr as $k=>$pic){
$h = curl_init();
curl_setopt($h,CURLOPT_URL,$pic['url']);
curl_setopt($h,CURLOPT_HEADER,0);
curl_setopt($h,CURLOPT_RETURNTRANSFER,1);//return the image value
array_push($hArr,$h);
}
$mh = curl_multi_init();
foreach($hArr as $k => $h) curl_multi_add_handle($mh,$h);
$running = null;
do{
curl_multi_exec($mh,$running);
}while($running > 0);
// get the result and save it in the result ARRAY
foreach($hArr as $k => $h){
$picsArr[$k]['data'] = curl_multi_getcontent($h);
}
//close all the connections
foreach($hArr as $k => $h){
$info = curl_getinfo($h);
preg_match("/^image\/(.*)$/",$info['content_type'],$matches);
echo $tail = $matches[1];
curl_multi_remove_handle($mh,$h);
}
curl_multi_close($mh);
return true;
}
?>