-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path11-http-blocking.php
More file actions
40 lines (33 loc) · 1.09 KB
/
11-http-blocking.php
File metadata and controls
40 lines (33 loc) · 1.09 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
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/',
);
function download(array $urls)
{
$browser = new React\Http\Browser();
$urls = array_combine($urls, $urls);
$promise = Clue\React\Mq\Queue::all(3, $urls, function ($url) use ($browser) {
return $browser->get($url)->then(
function (Psr\Http\Message\ResponseInterface $response) {
// return only the body for successful responses
return $response->getBody();
},
function (Exception $e) {
// return null body for failed requests
return null;
}
);
});
return React\Async\await($promise);
}
$responses = download($urls);
foreach ($responses as $url => $response) {
echo $url . ' is ' . strlen($response) . ' bytes' . PHP_EOL;
}