forked from clue/reactphp-mq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-http-all.php
35 lines (29 loc) · 1.04 KB
/
02-http-all.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
33
34
<?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.google.com/',
//'http://httpbin.org/delay/2',
);
$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
$promise = Clue\React\Mq\Queue::all(3, array_combine($urls, $urls), function ($url) use ($browser) {
return $browser->get($url);
});
$promise->then(
function ($responses) {
/* @var $responses Psr\Http\Message\ResponseInterface[] */
echo 'All URLs succeeded!' . PHP_EOL;
foreach ($responses as $url => $response) {
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
}
},
function ($e) {
echo 'An error occurred: ' . $e->getMessage() . PHP_EOL;
}
);