PHP Guzzle Proxy Integration
Guzzle is the standard HTTP client for PHP. It supports proxy configuration through request options, making it simple to route traffic through Hex Proxies for scraping, monitoring, or automation.
Basic Proxy Configuration
<?phpuse GuzzleHttp\Client;
$client = new Client([ 'proxy' => 'http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080', 'timeout' => 30, 'connect_timeout' => 10, ]);
$response = $client->get('https://httpbin.org/ip'); echo $response->getStatusCode() . "\n"; echo $response->getBody()->getContents() . "\n"; ```
Protocol-Specific Proxies
Guzzle allows separate proxy settings for HTTP and HTTPS:
$client = new Client([
'proxy' => [
'http' => 'http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080',
'https' => 'http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080',
'no' => ['localhost', '127.0.0.1'],
],
]);Sticky Sessions
Append a session tag to the username for consistent IP assignment across requests:
$sessionUser = 'YOUR_USERNAME-session-php001';
$client = new Client([
'proxy' => "http://{$sessionUser}:YOUR_PASSWORD@gate.hexproxies.com:8080",// Both requests use the same exit IP $page1 = $client->get('https://example.com/login'); $page2 = $client->get('https://example.com/dashboard'); ```
Error Handling and Retry
use GuzzleHttp\Exception\RequestException;function fetchWithRetry(Client $client, string $url, int $maxRetries = 3): string { $lastException = null;
for ($attempt = 1; $attempt <= $maxRetries; $attempt++) { try { $response = $client->get($url); if ($response->getStatusCode() === 200) { return $response->getBody()->getContents(); } } catch (ConnectException $e) { $lastException = $e; sleep(pow(2, $attempt)); } catch (RequestException $e) { $code = $e->getResponse()?->getStatusCode(); if ($code === 429 || $code === 503) { sleep(pow(2, $attempt)); continue; } throw $e; } }
throw $lastException ?? new \RuntimeException("Max retries exceeded for {$url}"); } ```
Concurrent Requests with Pool
use GuzzleHttp\Pool;$urls = [ 'https://httpbin.org/ip', 'https://httpbin.org/headers', 'https://httpbin.org/get', ];
$requests = function () use ($urls) { foreach ($urls as $url) { yield new Request('GET', $url); } };
$pool = new Pool($client, $requests(), [ 'concurrency' => 5, 'fulfilled' => function ($response, $index) { echo "Request {$index}: " . $response->getStatusCode() . "\n"; }, 'rejected' => function ($reason, $index) { echo "Request {$index} failed: " . $reason->getMessage() . "\n"; }, ]);
$pool->promise()->wait(); ```
SOCKS5 Support
Guzzle supports SOCKS5 via cURL under the hood:
$client = new Client([
'proxy' => 'socks5://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:1080',
'curl' => [
CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME,
],
]);Environment Variables
$proxyUrl = getenv('HEX_PROXY_URL');
$client = new Client(['proxy' => $proxyUrl]);