Skip to content

Instantly share code, notes, and snippets.

@sndsgd
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save sndsgd/3883c8af9aa2cd1669dc to your computer and use it in GitHub Desktop.

Select an option

Save sndsgd/3883c8af9aa2cd1669dc to your computer and use it in GitHub Desktop.
Perform multiple cURL requests in parallel with PHP
/**
* perform multiple curl requests in parallel
* @param array.<string> $urls - an indexed array of urls to fetch
* @param array.<integer> $curopts - curl options to pass to curl_setopt_array()
* @return array
*/
function curlMulti(array $urls, array $curlopts)
{
$ret = [];
$chs = [];
$mh = curl_multi_init();
$len = count($urls);
for ($i=0; $i<$len; $i++) {
$chs[$i] = curl_init($urls[$i]);
curl_setopt_array($chs[$i], $curlopts);
curl_multi_add_handle($mh, $chs[$i]);
}
do {
curl_multi_exec($mh, $active);
curl_multi_select($mh, .1);
}
while ($active);
for ($i=0; $i<$len; $i++) {
$info = curl_getinfo($chs[$i]);
$ret[$info['url']] = [
'info' => $info,
'content' => mb_convert_encoding(
curl_multi_getcontent($chs[$i]),
'HTML-ENTITIES',
'UTF-8'
)
];
}
curl_multi_close($mh);
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment