-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsymfony_http_client_proxy.php
More file actions
37 lines (30 loc) · 1.02 KB
/
symfony_http_client_proxy.php
File metadata and controls
37 lines (30 loc) · 1.02 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
#!/usr/bin/env php
<?php
/**
* Symfony HttpClient with proxy example.
*
* Configuration via environment variables:
* PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
* TEST_URL - URL to request (default: https://api.ipify.org?format=json)
*
* Symfony HttpClient is a modern, PSR-18 compatible HTTP client.
* It supports proxies but does NOT support custom CONNECT headers or
* reading proxy response headers.
*/
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/common.php';
use Symfony\Component\HttpClient\HttpClient;
$proxyUrl = get_proxy_url();
$testUrl = getenv('TEST_URL') ?: 'https://api.ipify.org?format=json';
try {
$client = HttpClient::create([
'proxy' => $proxyUrl,
'timeout' => 30,
]);
$response = $client->request('GET', $testUrl);
echo "Status: " . $response->getStatusCode() . "\n";
echo "Body: " . $response->getContent() . "\n";
} catch (Exception $e) {
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
exit(1);
}