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.