|
| 1 | +<?php |
| 2 | + |
| 3 | +// A simple example which requests http://google.com/ directly (optional: Through a SOCKS proxy.) |
| 4 | +// To run the example, go to the project root and run: |
| 5 | +// |
| 6 | +// $ php examples/12-optional-proxy-raw-http-protocol.php |
| 7 | +// |
| 8 | +// If you chose the optional route, you can use any kind of proxy, for example https://github.com/leproxy/leproxy (defaults to localhost:8080) and execute it like this: |
| 9 | +// |
| 10 | +// $ php leproxy.php |
| 11 | +// |
| 12 | +// To run the same example with your proxy, the proxy URL can be given as an environment variable: |
| 13 | +// |
| 14 | +// $ socks_proxy=127.0.0.2:1080 php examples/12-optional-proxy-raw-http-protocol.php |
| 15 | +// |
| 16 | +// This example highlights how changing from direct connection to using a proxy |
| 17 | +// actually adds very little complexity and does not mess with your actual |
| 18 | +// network protocol otherwise. |
| 19 | +// |
| 20 | +// For illustration purposes only. If you want to send HTTP requests in a real |
| 21 | +// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. |
| 22 | + |
| 23 | +require __DIR__ . '/../vendor/autoload.php'; |
| 24 | + |
| 25 | +$loop = React\EventLoop\Factory::create(); |
| 26 | + |
| 27 | +$connector = new React\Socket\Connector($loop); |
| 28 | + |
| 29 | +$url = getenv('socks_proxy'); |
| 30 | +if ($url !== false) { |
| 31 | + $client = new Clue\React\Socks\Client($url, $connector); |
| 32 | + $connector = new React\Socket\Connector($loop, array( |
| 33 | + 'tcp' => $client, |
| 34 | + 'timeout' => 3.0, |
| 35 | + 'dns' => false |
| 36 | + )); |
| 37 | +} |
| 38 | + |
| 39 | +echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL; |
| 40 | + |
| 41 | +$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $stream) { |
| 42 | + echo 'connected' . PHP_EOL; |
| 43 | + $stream->write("GET / HTTP/1.0\r\n\r\n"); |
| 44 | + $stream->on('data', function ($data) { |
| 45 | + echo $data; |
| 46 | + }); |
| 47 | +}, function (Exception $e) { |
| 48 | + echo 'Error: ' . $e->getMessage() . PHP_EOL; |
| 49 | +}); |
| 50 | + |
| 51 | +$loop->run(); |
0 commit comments