|
1 | 1 | <?php |
2 | 2 |
|
3 | | -// A simple example which requests http://google.com/ either directly or through |
4 | | -// an SSH proxy server. |
| 3 | +// A simple example which requests http://google.com/ either directly or through an SSH proxy server. |
5 | 4 | // The proxy can be given through the SSH_PROXY env or does not use a proxy otherwise. |
6 | | -// This example highlights how changing from direct connection to using a proxy |
7 | | -// actually adds very little complexity and does not mess with your actual |
8 | | -// network protocol otherwise. |
9 | 5 | // |
10 | 6 | // You can assign the SSH_PROXY environment and prefix this with a space to make |
11 | 7 | // sure your login credentials are not stored in your bash history like this: |
12 | 8 | // |
13 | 9 | // $ export SSH_PROXY=user:[email protected] |
14 | | -// $ php examples/02-optional-proxy-http.php |
| 10 | +// $ php examples/12-optional-proxy-raw-http-protocol.php |
| 11 | +// |
| 12 | +// This example highlights how changing from direct connection to using a proxy |
| 13 | +// actually adds very little complexity and does not mess with your actual |
| 14 | +// network protocol otherwise. |
15 | 15 | // |
16 | 16 | // For illustration purposes only. If you want to send HTTP requests in a real |
17 | | -// world project, take a look at https://github.com/clue/reactphp-buzz#ssh-proxy |
18 | | - |
19 | | -use Clue\React\SshProxy\SshProcessConnector; |
20 | | -use React\Socket\Connector; |
21 | | -use React\Socket\ConnectionInterface; |
| 17 | +// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. |
22 | 18 |
|
23 | 19 | require __DIR__ . '/../vendor/autoload.php'; |
24 | 20 |
|
25 | 21 | $loop = React\EventLoop\Factory::create(); |
26 | 22 |
|
27 | 23 | // SSH_PROXY environment given? use this as the proxy URL |
28 | 24 | if (getenv('SSH_PROXY') !== false) { |
29 | | - $proxy = new SshProcessConnector(getenv('SSH_PROXY'), $loop); |
30 | | - $connector = new Connector($loop, array( |
| 25 | + $proxy = new Clue\React\SshProxy\SshProcessConnector(getenv('SSH_PROXY'), $loop); |
| 26 | + $connector = new React\Socket\Connector($loop, array( |
31 | 27 | 'tcp' => $proxy, |
32 | 28 | 'timeout' => 3.0, |
33 | 29 | 'dns' => false |
34 | 30 | )); |
35 | 31 | } else { |
36 | | - $connector = new Connector($loop); |
| 32 | + $connector = new React\Socket\Connector($loop); |
37 | 33 | } |
38 | 34 |
|
39 | | -$connector->connect('tcp://google.com:80')->then(function (ConnectionInterface $stream) { |
| 35 | +$connector->connect('tcp://google.com:80')->then(function (React\Socket\ConnectionInterface $stream) { |
40 | 36 | $stream->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n"); |
41 | 37 | $stream->on('data', function ($chunk) { |
42 | 38 | echo $chunk; |
43 | 39 | }); |
44 | | -}, 'printf'); |
| 40 | +}, function (Exception $e) { |
| 41 | + echo 'Error: ' . $e->getMessage() . PHP_EOL; |
| 42 | +}); |
45 | 43 |
|
46 | 44 | $loop->run(); |
0 commit comments