forked from clue/reactphp-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-http-request.php
More file actions
40 lines (32 loc) · 1.21 KB
/
01-http-request.php
File metadata and controls
40 lines (32 loc) · 1.21 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
37
38
39
40
<?php
// A simple example which uses an HTTP client to request https://example.com/ through an HTTP CONNECT proxy.
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
//
// $ php leproxy.php
//
// The proxy defaults to localhost:8080.
// To run the example go to the project root and run:
//
// $ php examples/01-http-request.php
//
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
//
// $ http_proxy=127.0.0.2:8080 php examples/01-http-request.php
require __DIR__ . '/../vendor/autoload.php';
$url = getenv('http_proxy');
if ($url === false) {
$url = 'localhost:8080';
}
$loop = React\EventLoop\Factory::create();
$proxy = new Clue\React\HttpProxy\ProxyConnector($url, new React\Socket\Connector($loop));
$connector = new React\Socket\Connector($loop, array(
'tcp' => $proxy,
'dns' => false
));
$browser = new React\Http\Browser($loop, $connector);
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
$loop->run();