forked from amphp/socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-http-client.php
42 lines (32 loc) · 1.34 KB
/
simple-http-client.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
37
38
39
40
41
42
<?php // basic (and dumb) HTTP client
require __DIR__ . '/../vendor/autoload.php';
// This is a very simple HTTP client that just prints the response without parsing.
use Amp\ByteStream\ResourceOutputStream;
use Amp\Loop;
use Amp\Socket\Socket;
use Amp\Uri\Uri;
use function Amp\Socket\connect;
use function Amp\Socket\cryptoConnect;
Loop::run(function () use ($argv) {
$stdout = new ResourceOutputStream(STDOUT);
if (count($argv) !== 2) {
yield $stdout->write("Usage: examples/simple-http-client.php url" . PHP_EOL);
exit(1);
}
$uri = new Uri($argv[1]);
$host = $uri->getHost();
if ($uri->getScheme() === "https") {
/** @var Socket $socket */
$socket = yield cryptoConnect("tcp://" . $host . ":" . $uri->getPort());
} else {
/** @var Socket $socket */
$socket = yield connect("tcp://" . $host . ":" . $uri->getPort());
}
yield $socket->write("GET {$uri} HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n");
while (null !== $chunk = yield $socket->read()) {
yield $stdout->write($chunk);
}
// If the promise returned from `read()` resolves to `null`, the socket closed and we're done.
// In this case you can also use `yield Amp\ByteStream\pipe($socket, $stdout)` instead of the while loop,
// but we want to demonstate the `read()` method here.
});