v1.8.91-d84675c
← Back to Hex Proxies

PHP Guzzle Proxy Setup

Last updated: April 2026

By Hex Proxies Engineering Team

Configure Hex Proxies in PHP applications using Guzzle. Covers proxy authentication, HTTPS tunneling, sticky sessions, and error handling.

intermediate15 minuteslanguage-integration

Prerequisites

  • PHP 8.1 or later
  • Guzzle 7.x installed via Composer
  • Hex Proxies account with credentials

Steps

1

Install Guzzle

Run composer require guzzlehttp/guzzle to add Guzzle 7.x to your project.

2

Configure the proxy

Pass the proxy option with gate.hexproxies.com credentials when creating the Client.

3

Test connectivity

Send a GET request to httpbin.org/ip and verify the response shows a proxied IP.

4

Add session support

Append a session tag to the username for sticky IP assignment.

5

Implement error handling

Catch ConnectException and RequestException with retry logic and backoff.

6

Scale with Pool

Use Guzzle Pool for concurrent proxied requests with controlled concurrency.

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

<?php

use 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]);

Tips

  • *Use the no key in the proxy array to bypass the proxy for local or internal requests.
  • *Guzzle uses cURL by default -- SOCKS5 support requires the cURL extension.
  • *Set both timeout and connect_timeout to prevent long-running requests from blocking.
  • *Read proxy credentials from environment variables to keep them out of source code.

Ready to Get Started?

Put this guide into practice with Hex Proxies.

Cookie Preferences

We use cookies to ensure the best experience. You can customize your preferences below. Learn more