-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path01-http.php
32 lines (27 loc) · 960 Bytes
/
01-http.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
require __DIR__ . '/../vendor/autoload.php';
// list of all URLs you want to download
// this list may potentially contain hundreds or thousands of entries
$urls = array(
'http://www.github.com/',
'http://www.yahoo.com/',
'http://www.bing.com/',
'http://www.bing.com/invalid',
'http://www.google.com/',
);
$browser = new React\Http\Browser();
// each job should use the browser to GET a certain URL
// limit number of concurrent jobs here to avoid using excessive network resources
$queue = new Clue\React\Mq\Queue(3, null, function ($url) use ($browser) {
return $browser->get($url);
});
foreach ($urls as $url) {
$queue($url)->then(
function (Psr\Http\Message\ResponseInterface $response) use ($url) {
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
},
function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);
}