Last active
March 22, 2018 12:55
-
-
Save stefpe/2ee68ab77bf8423bdaa3ef06dc69015c to your computer and use it in GitHub Desktop.
Multi 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 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