Skip to content

Instantly share code, notes, and snippets.

@robertodormepoco
Last active August 29, 2015 14:22
Show Gist options
  • Save robertodormepoco/715e20b25d61a9138c61 to your computer and use it in GitHub Desktop.
Save robertodormepoco/715e20b25d61a9138c61 to your computer and use it in GitHub Desktop.
curl multi
<?php
$urls = [
'http://localhost/late.php?sec=5',
'http://localhost/late.php?sec=4',
'http://www.google.com/index.php',
'http://localhost/late.php?sec=3',
'http://localhost/late.php?sec=3',
'http://localhost/late.php?sec=3',
'http://localhost/late.php?sec=3',
'http://www.repubblica.it',
'http://localhost/late.php?sec=2',
'http://localhost/late.php?sec=1'
];
$responses = [];
$resources = [];
$mh = curl_multi_init();
foreach($urls as $k => $url) {
$resources[$k] = curl_init();
curl_setopt($resources[$k], CURLOPT_URL, $url);
curl_setopt($resources[$k], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resources[$k], CURLOPT_HEADER, 0);
curl_multi_add_handle($mh, $resources[$k]);
}
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) == -1) {usleep(50);}
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
$info = curl_multi_info_read($mh, $left);
if ($info['result'] == CURLE_OK && $info['msg'] == CURLMSG_DONE){
print_r(curl_multi_getcontent($info['handle']));
}
}
for($i = 0; $i < $left; $i++){
$info = curl_multi_info_read($mh, $count);
if ($info['result'] == CURLE_OK && $info['msg'] == CURLMSG_DONE){
print_r(curl_multi_getcontent($info['handle']));
}
}
foreach($urls as $k => $url) {
curl_multi_remove_handle($mh, $resources[$k]);
}
curl_multi_close($mh);
<?php
sleep($_GET['sec']);
$html = <<<HTML
<html>
<body>
yo %s
</body>
</html>
HTML;
echo sprintf($html, $_GET['sec']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment