Skip to content

Instantly share code, notes, and snippets.

@stefpe
Last active March 22, 2018 12:55
Show Gist options
  • Save stefpe/2ee68ab77bf8423bdaa3ef06dc69015c to your computer and use it in GitHub Desktop.
Save stefpe/2ee68ab77bf8423bdaa3ef06dc69015c to your computer and use it in GitHub Desktop.
Multi curl
function multiRequest(array $requests = []): array
{
$std_options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5
];
$mh = curl_multi_init();
$handlers = [];
foreach ($requests as $url) {
$handler = curl_init($url);
curl_setopt_array($handler, $std_options);
$handlers[] = $handler;
curl_multi_add_handle($mh, $handler);
}
$active = null;
$result = [];
do {
do {
$execrun = curl_multi_exec($mh, $active);
} while ($execrun == CURLM_CALL_MULTI_PERFORM);
} while ($active);
foreach ($handlers as $i => $handler) {
$result[$i] = curl_multi_getcontent($handler);
curl_multi_remove_handle($mh, $handler);
}
curl_multi_close($mh);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment