Created
August 15, 2019 13:26
-
-
Save brannow/543b75d989f89984bfa37c96aba41c05 to your computer and use it in GitHub Desktop.
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
<?php declare(strict_types=1); | |
function curlSingle(string $url, callable $block) | |
{ | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
$result = curl_exec($ch); | |
$chInfo = curl_getinfo($ch); | |
if ($chInfo['http_code'] === 200) { | |
$block($result, $chInfo); | |
} | |
curl_close($ch); | |
} | |
/** | |
* @param array $urls | |
* @param callable $block | |
*/ | |
function curlMulti(array $urls, callable $block) | |
{ | |
$master = curl_multi_init(); | |
foreach ($urls as $url) { | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_multi_add_handle($master, $ch); | |
} | |
$status = CURLM_OK; | |
$running = true; | |
do { | |
curl_multi_exec($master, $running); | |
$queueCount = 0; | |
while ($info = curl_multi_info_read($master, $queueCount)) { | |
if ($info['msg'] === CURLMSG_DONE && $info['result'] === CURLE_OK) { | |
$ch = $info['handle']; | |
$chInfo = curl_getinfo($ch); | |
if ($chInfo['http_code'] === 200) { | |
$result = curl_multi_getcontent($ch); | |
$block($result, $chInfo); | |
} | |
} | |
} | |
} while ($running && $status == CURLM_OK); | |
curl_multi_close($master); | |
} | |
/** | |
* | |
* SEND SYNC CURL REQUESTS | |
*/ | |
$t = microtime(true); | |
$c = 0; | |
$block = function ($result, $header) { | |
global $c; | |
$c++; | |
echo '['.$c.'] size: '. strlen($result) . PHP_EOL; | |
}; | |
for ($i = 0; $i < 50; ++$i) { | |
curlSingle('https://www.bing.de?q=test+'.$i, $block); | |
} | |
echo microtime(true) - $t .' seconds'. PHP_EOL; | |
/** | |
* | |
* SEND ASYNC CURL REQUESTS | |
*/ | |
$t = microtime(true); | |
$urls = []; | |
for ($i = 0; $i < 50; ++$i) { | |
$urls[] = 'https://www.bing.de?q=test+'.$i; | |
} | |
$c = 0; | |
$block = function ($result, $header) { | |
global $c; | |
$c++; | |
echo '['.$c.'] size: '. strlen($result) . PHP_EOL; | |
}; | |
curlMulti($urls, $block); | |
echo microtime(true) - $t .' seconds'. PHP_EOL; | |
/** | |
... | |
[49] size: 161713 | |
[50] size: 123104 | |
25.498881101608 seconds | |
ASYNC: | |
... | |
[49] size: 182714 | |
[50] size: 171094 | |
3.1386489868164 seconds | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment