-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
22-http-client.php
58 lines (46 loc) · 1.9 KB
/
22-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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
// Simple plaintext HTTP and secure HTTPS client example (for illustration purposes only).
// This shows how an URI parameter can parsed to decide whether to establish
// a plaintext TCP/IP or secure TLS connection and then send an
// application level protocol message (HTTP).
// Real applications should use react/http-client instead!
//
// Unlike examples #11 and #12, this example will actually take an optional URI
// parameter and parse it to connect to the correct default port and use the
// correct transport protocol.
//
// $ php examples/22-http-client.php
// $ php examples/22-http-client.php https://reactphp.org/
use React\Socket\ConnectionInterface;
use React\Socket\Connector;
use React\Stream\WritableResourceStream;
require __DIR__ . '/../vendor/autoload.php';
$uri = $argv[1] ?? 'www.google.com';
if (strpos($uri, '://') === false) {
$uri = 'http://' . $uri;
}
$parts = parse_url($uri);
if (!$parts || !isset($parts['scheme'], $parts['host'])) {
fwrite(STDERR, 'Usage error: required argument <host:port>' . PHP_EOL);
exit(1);
}
$connector = new Connector();
if (!isset($parts['port'])) {
$parts['port'] = $parts['scheme'] === 'https' ? 443 : 80;
}
$host = $parts['host'];
if (($parts['scheme'] === 'http' && $parts['port'] !== 80) || ($parts['scheme'] === 'https' && $parts['port'] !== 443)) {
$host .= ':' . $parts['port'];
}
$target = ($parts['scheme'] === 'https' ? 'tls' : 'tcp') . '://' . $parts['host'] . ':' . $parts['port'];
$resource = $parts['path'] ?? '/';
if (isset($parts['query'])) {
$resource .= '?' . $parts['query'];
}
$stdout = new WritableResourceStream(STDOUT);
$connector->connect($target)->then(function (ConnectionInterface $connection) use ($resource, $host, $stdout) {
$connection->pipe($stdout);
$connection->write("GET $resource HTTP/1.0\r\nHost: $host\r\n\r\n");
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});