-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path03-http-any.php
36 lines (31 loc) · 1.04 KB
/
03-http-any.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
35
36
<?php
require __DIR__ . '/../vendor/autoload.php';
// list of all URLs you want to try
// this list may potentially contain hundreds or thousands of entries
$urls = array(
'http://www.github.com/invalid',
'http://www.yahoo.com/invalid',
'http://www.bing.com/invalid',
'http://www.bing.com/',
'http://www.google.com/',
'http://www.google.com/invalid',
);
$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::any(2, $urls, function ($url) use ($browser) {
return $browser->get($url)->then(
function (Psr\Http\Message\ResponseInterface $response) use ($url) {
// return only the URL for the first successful response
return $url;
}
);
});
$promise->then(
function ($url) {
echo 'First successful URL is ' . $url . PHP_EOL;
},
function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);