Created
January 17, 2012 19:39
-
-
Save saadullahsaeed/1628395 to your computer and use it in GitHub Desktop.
PHP function to do parallel curl requests
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 threadedRequests($urls){ | |
$mh = curl_multi_init(); | |
$curl_array = array(); | |
foreach($urls as $i => $url){ | |
$curl_array[$i] = curl_init($url); | |
curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true); | |
curl_multi_add_handle($mh, $curl_array[$i]); | |
} | |
$running = NULL; | |
do { | |
curl_multi_exec($mh,$running); | |
} while($running > 0); | |
$res = array(); | |
foreach($urls as $i => $url){ | |
$res[$i] = curl_multi_getcontent($curl_array[$i]); | |
curl_multi_remove_handle($mh, $curl_array[$i]); | |
} | |
curl_multi_close($mh); | |
return $res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment