-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
03-any.php
32 lines (24 loc) · 918 Bytes
/
03-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
<?php
// concurrently request a number of URLs.
// return immediately once the first is completed, cancel all others.
use Clue\React\Buzz\Browser;
use Psr\Http\Message\ResponseInterface;
require __DIR__ . '/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$client = new Browser($loop);
$promises = array(
$client->head('http://www.github.com/clue/http-react'),
$client->get('https://httpbin.org/'),
$client->get('https://google.com'),
$client->get('http://www.lueck.tv/psocksd'),
$client->get('http://www.httpbin.org/absolute-redirect/5')
);
React\Promise\any($promises)->then(function (ResponseInterface $response) use ($promises) {
// first response arrived => cancel all other pending requests
foreach ($promises as $promise) {
$promise->cancel();
}
var_dump($response->getHeaders());
echo PHP_EOL . $response->getBody();
});
$loop->run();