Skip to content

Instantly share code, notes, and snippets.

@saadullahsaeed
Created January 17, 2012 19:39
Show Gist options
  • Save saadullahsaeed/1628395 to your computer and use it in GitHub Desktop.
Save saadullahsaeed/1628395 to your computer and use it in GitHub Desktop.
PHP function to do parallel curl requests
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