-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
11-proxy-raw-http-protocol.php
45 lines (38 loc) · 1.46 KB
/
11-proxy-raw-http-protocol.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
<?php
// A simple example which requests http://google.com/ through a SOCKS proxy.
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy (defaults to localhost:8080) and execute it like this:
//
// $ php leproxy.php
//
// The proxy in this example defaults to 127.0.0.1:1080.
// To run the example, go to the project root and run:
//
// $ php examples/11-proxy-raw-http-protocol.php
//
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
//
// $ socks_proxy=127.0.0.2:1080 php examples/11-proxy-raw-http-protocol.php
//
// For illustration purposes only. If you want to send HTTP requests in a real
// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage.
require __DIR__ . '/../vendor/autoload.php';
$url = getenv('socks_proxy');
if ($url === false) {
$url = '127.0.0.1:1080';
}
$proxy = new Clue\React\Socks\Client($url);
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));
echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL;
$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
echo 'connected' . PHP_EOL;
$connection->write("GET / HTTP/1.0\r\n\r\n");
$connection->on('data', function ($data) {
echo $data;
});
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});