|
3 | 3 | // Simple plaintext HTTP client example (for illustration purposes only). |
4 | 4 | // This shows how a plaintext TCP/IP connection is established to then send an |
5 | 5 | // application level protocol message (HTTP). |
6 | | -// Real applications should use react/http-client instead |
| 6 | +// Real applications should use react/http-client instead! |
| 7 | +// |
| 8 | +// This simple example only accepts an optional host parameter to send the |
| 9 | +// request to. See also example #22 for proper URI parsing. |
| 10 | +// |
| 11 | +// $ php examples/11-http-client.php |
| 12 | +// $ php examples/11-http-client.php reactphp.org |
7 | 13 |
|
8 | 14 | use React\EventLoop\Factory; |
9 | 15 | use React\Socket\Connector; |
10 | 16 | use React\Socket\ConnectionInterface; |
11 | 17 |
|
12 | | -$target = isset($argv[1]) ? $argv[1] : 'www.google.com:80'; |
| 18 | +$host = isset($argv[1]) ? $argv[1] : 'www.google.com'; |
13 | 19 |
|
14 | 20 | require __DIR__ . '/../vendor/autoload.php'; |
15 | 21 |
|
16 | 22 | $loop = Factory::create(); |
17 | 23 | $connector = new Connector($loop); |
18 | 24 |
|
19 | | -$connector->connect($target)->then(function (ConnectionInterface $connection) use ($target) { |
| 25 | +$connector->connect($host. ':80')->then(function (ConnectionInterface $connection) use ($host) { |
20 | 26 | $connection->on('data', function ($data) { |
21 | 27 | echo $data; |
22 | 28 | }); |
23 | 29 | $connection->on('close', function () { |
24 | 30 | echo '[CLOSED]' . PHP_EOL; |
25 | 31 | }); |
26 | 32 |
|
27 | | - $connection->write("GET / HTTP/1.0\r\nHost: $target\r\n\r\n"); |
| 33 | + $connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n"); |
28 | 34 | }, 'printf'); |
29 | 35 |
|
30 | 36 | $loop->run(); |
0 commit comments