v1.10.82-f67ee7d
Skip to main content
← Back to Hex Proxies

PHP cURL Proxy Integration

Configure PHP cURL to route requests through Hex Proxies.

PHP cURL Proxy Setup

PHP's cURL extension is the standard HTTP client for PHP applications. It wraps libcurl, providing access to the same proxy options available in the command-line curl tool. Configuring Hex Proxies with PHP cURL routes all HTTP requests through residential IPs for web scraping, API access, and data collection workflows.

Complete Configuration Example

<?php
$url = 'https://httpbin.org/ip';
$proxyUser = getenv('PROXY_USER');

$ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_PROXY => 'gate.hexproxies.com:8080', CURLOPT_PROXYUSERPWD => "$proxyUser:$proxyPass", CURLOPT_PROXYTYPE => CURLPROXY_HTTP, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 15, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => [ 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)', 'Accept-Language: en-US,en;q=0.9', ], CURLOPT_SSL_VERIFYPEER => true, ]);

$response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $totalTime = curl_getinfo($ch, CURLINFO_TOTAL_TIME);

if (curl_errno($ch)) { echo "Error: " . curl_error($ch) . "\n"; } else { echo "[$httpCode] Response in {$totalTime}s\n"; echo $response . "\n"; }

curl_close($ch); ```

Reusable Proxy Function with Retry Logic

<?php
function fetchWithProxy(string $url, int $maxRetries = 3): ?string {
    $proxyUrl = 'gate.hexproxies.com:8080';

for ($attempt = 1; $attempt <= $maxRetries; $attempt++) { $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_PROXY => $proxyUrl, CURLOPT_PROXYUSERPWD => $proxyAuth, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 15, CURLOPT_TIMEOUT => 30, ]);

$response = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);

if ($code === 200) return $response; if (in_array($code, [403, 429, 503])) { sleep($attempt * 2); // exponential backoff continue; } break; } return null; } ```

Geo-Targeted Requests

Append a country code to the proxy username for geo-targeted requests:

$geoUser = getenv('PROXY_USER') . '-country-de';
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$geoUser:$proxyPass");

Common Pitfalls

Forgetting `CURLOPT_RETURNTRANSFER` causes curl_exec to output directly instead of returning the response. Always set it to true. Also set `CURLOPT_CONNECTTIMEOUT` explicitly — the default is 0 (no timeout), which can cause PHP scripts to hang indefinitely if the proxy is unreachable.

For HTTPS targets, PHP cURL verifies SSL certificates through the proxy tunnel. If you get SSL errors, update your CA bundle (`CURLOPT_CAINFO`) rather than disabling verification.

SOCKS5 Support

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);

Use `CURLPROXY_SOCKS5_HOSTNAME` (not `CURLPROXY_SOCKS5`) to resolve DNS through the proxy, preventing DNS leaks.

Integration Steps

1

Initialize cURL with proxy options

Create a cURL handle and set CURLOPT_PROXY, CURLOPT_PROXYUSERPWD, and CURLOPT_PROXYTYPE. Store credentials in environment variables, not source code.

2

Configure timeouts for proxy latency

Set CURLOPT_CONNECTTIMEOUT to 15 seconds and CURLOPT_TIMEOUT to 30 seconds to account for residential proxy handshake overhead.

3

Add retry logic for blocked requests

Check the HTTP status code after each request. Retry on 403, 429, and 503 with exponential backoff to handle transient proxy blocks.

4

Verify proxy routing

Fetch https://httpbin.org/ip and confirm the response shows a residential IP, not your server IP.

Operational Tips

Keep sessions stable for workflows that depend on consistent identity. For high-volume collection, rotate IPs and reduce concurrency if you see timeouts or 403 responses.

  • Prefer sticky sessions for multi-step flows (auth, checkout, forms).
  • Rotate per request for scale and broad coverage.
  • Use timeouts and retries to handle transient failures.

Frequently Asked Questions

Can I use SOCKS5 with PHP cURL?

Yes. Set CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME to route through SOCKS5 with remote DNS resolution, preventing DNS leaks.

Why does my PHP cURL proxy request hang?

The default connect timeout is unlimited. Set CURLOPT_CONNECTTIMEOUT to 15 seconds and CURLOPT_TIMEOUT to 30 seconds to fail fast on unresponsive proxy connections.

How do I rotate IPs in PHP cURL?

Append a unique session ID to the proxy username for each request (e.g., user-session-abc123). The Hex Proxies gateway assigns a different residential IP per session ID.

Ready to Integrate?

Start using residential proxies with PHP cURL today.