-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy path21-stream-forwarding.php
33 lines (24 loc) · 959 Bytes
/
21-stream-forwarding.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
<?php
use Clue\React\Buzz\Browser;
use Psr\Http\Message\ResponseInterface;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableResourceStream;
use RingCentral\Psr7;
require __DIR__ . '/../vendor/autoload.php';
if (DIRECTORY_SEPARATOR === '\\') {
fwrite(STDERR, 'Non-blocking console I/O not supported on Windows' . PHP_EOL);
exit(1);
}
$loop = React\EventLoop\Factory::create();
$client = new Browser($loop);
$out = new WritableResourceStream(STDOUT, $loop);
$info = new WritableResourceStream(STDERR, $loop);
$url = isset($argv[1]) ? $argv[1] : 'http://google.com/';
$info->write('Requesting ' . $url . '…' . PHP_EOL);
$client->requestStreaming('GET', $url)->then(function (ResponseInterface $response) use ($info, $out) {
$info->write('Received' . PHP_EOL . Psr7\str($response));
$body = $response->getBody();
assert($body instanceof ReadableStreamInterface);
$body->pipe($out);
}, 'printf');
$loop->run();