forked from clue/reactphp-mq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-http.php
More file actions
40 lines (33 loc) · 1.08 KB
/
01-http.php
File metadata and controls
40 lines (33 loc) · 1.08 KB
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
35
36
37
38
39
40
<?php
use Clue\React\Mq\Queue;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory;
use React\Http\Browser;
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/',
);
$loop = Factory::create();
$browser = new Browser($loop);
// 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 Queue(3, null, function ($url) use ($browser) {
return $browser->get($url);
});
foreach ($urls as $url) {
$queue($url)->then(
function (ResponseInterface $response) use ($url) {
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
},
function (Exception $e) use ($url) {
echo $url . ' failed: ' . $e->getMessage() . PHP_EOL;
}
);
}
$loop->run();