forked from reactphp/stream
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01-http.php
More file actions
36 lines (28 loc) · 1.03 KB
/
01-http.php
File metadata and controls
36 lines (28 loc) · 1.03 KB
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
// Simple plaintext HTTP client example (for illustration purposes only).
// This shows how a plaintext TCP/IP connection is established to then send an
// application level protocol message (HTTP).
// Real applications should use react/http-client instead!
//
// This simple example only accepts an optional host parameter to send the
// request to.
//
// $ php examples/01-http.php
// $ php examples/01-http.php reactphp.org
use React\Stream\DuplexResourceStream;
require __DIR__ . '/../vendor/autoload.php';
$host = $argv[1] ?? 'www.google.com';
// connect to tcp://www.google.com:80 (blocking call!)
// for illustration purposes only, should use react/http-client or react/socket instead!
$resource = stream_socket_client('tcp://' . $host . ':80');
if (!$resource) {
exit(1);
}
$stream = new DuplexResourceStream($resource);
$stream->on('data', function (string $chunk): void {
echo $chunk;
});
$stream->on('close', function (): void {
echo '[CLOSED]' . PHP_EOL;
});
$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");