Last active
September 27, 2015 10:28
-
-
Save kijtra/1255724 to your computer and use it in GitHub Desktop.
[PHP] multiRequest関数をgetのみに絞り、少し調整したもの。 http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/
This file contains 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
function multi_get($urls,$options=array()){ | |
$result=array(); | |
$mh=curl_multi_init(); | |
$curly=array(); | |
foreach($urls as $key=>$url){ | |
if(empty($url) || is_array($url) || !preg_match('/^(ftp|http)/',$url)){ | |
if (is_readable($url)) { | |
$result[$key] = file_get_contents($url); | |
} | |
continue; | |
} | |
$curly[$key]=curl_init(); | |
curl_setopt($curly[$key],CURLOPT_URL,$url); | |
curl_setopt($curly[$key],CURLOPT_HEADER,0); | |
curl_setopt($curly[$key],CURLOPT_RETURNTRANSFER,1); | |
if(!empty($options)){ | |
curl_setopt_array($curly[$key],$options); | |
} | |
curl_multi_add_handle($mh,$curly[$key]); | |
} | |
if(!empty($curly)){ | |
$running=NULL; | |
do { | |
curl_multi_exec($mh,$running); | |
}while($running>0); | |
foreach($curly as $key=>$c){ | |
if(empty($c)){ | |
continue; | |
} | |
$result[$key]=curl_multi_getcontent($c); | |
curl_multi_remove_handle($mh,$c); | |
} | |
} | |
curl_multi_close($mh); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment