-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy path8-proxy.php
More file actions
46 lines (36 loc) · 1.77 KB
/
8-proxy.php
File metadata and controls
46 lines (36 loc) · 1.77 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
41
42
43
44
45
46
<?php
use Amp\Http\Client\Connection\UnlimitedConnectionPool;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\Tunnel\Http1Tunnel;
use Amp\Loop;
use Amp\Socket\SocketAddress;
require __DIR__ . '/../.helper/functions.php';
Loop::run(static function () use ($argv) {
try {
// We only support direct TCP connection to the proxy, no TLS, because TLS in TLS is not supported
// Use a tunnel to the proxy if you need a encrypted connection to the proxy itself
$connector = new Http1Tunnel(new SocketAddress('127.0.0.1', 5512));
// If you need authentication, you can set a custom header (using Basic auth here)
// $connector = new Http1Tunnel(new SocketAddress('127.0.0.1', 5512), [
// 'proxy-authorization' => 'Basic ' . \base64_encode('user:pass'),
// ]);
// If you have a proxy accepting HTTPS connections, you need to use Https1Tunnel instead:
// $connector = new Https1Tunnel(new SocketAddress('proxy.example.com', 5512));
$client = (new HttpClientBuilder)
->usingPool(new UnlimitedConnectionPool($connector))
->build();
$request = new Request('http://amphp.org/');
/** @var Response $response */
$response = yield $client->request($request);
dumpRequestTrace($response->getRequest());
dumpResponseTrace($response);
dumpResponseBodyPreview(yield $response->getBody()->buffer());
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});