PHP cURL proxy examples

PHP cURL Proxy: Authentication, SOCKS and Error Handling

PHP’s cURL extension can send an HTTP request through a proxy by setting CURLOPT_PROXY. Production code should also set the proxy type, credentials, connection and total timeouts, TLS verification and explicit error handling.

BuyProxies title card reading PHP Proxies
Configure the proxy, execute with timeouts and TLS verification, then inspect cURL errors, HTTP status, exit IP and timing.

Quick answer: Initialize a cURL handle, set the destination URL, set CURLOPT_PROXY to host:port, add separate CURLOPT_PROXYUSERNAME and CURLOPT_PROXYPASSWORD values when credentials are required, keep CURLOPT_SSL_VERIFYPEER enabled, and inspect both curl_errno() and the HTTP response code. Never disable certificate verification as a normal fix.

Set CURLOPT_PROXY in a PHP cURL request

The examples use ipify’s public IPv4 JSON endpoint for the exit-IP check. It returns an object containing an ip field. You can substitute an equivalent JSON endpoint you control; test the real destination separately after confirming the proxy route.

Which PHP cURL option should hold the proxy password? Set CURLOPT_PROXY to the proxy endpoint. Pass the username and password as literal values in CURLOPT_PROXYUSERNAME and CURLOPT_PROXYPASSWORD. CURLOPT_USERPWD is for the destination server. Keep proxy credentials separate from any API login.

If you use the combined CURLOPT_PROXYUSERPWD option, libcurl URL-decodes both fields. Encode each field separately and join the results with a literal colon, as in rawurlencode($proxyUser) . ":" . rawurlencode($proxyPass). A literal %2F in a password becomes / if passed unencoded in the combined option.

<?php
$curl = curl_init('https://api.ipify.org?format=json');

curl_setopt_array($curl, [
    CURLOPT_PROXY => 'proxy.example:8080',
    CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30,
]);

$body = curl_exec($curl);

if ($body === false) {
    throw new RuntimeException(curl_error($curl), curl_errno($curl));
}

$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);

echo "HTTP status: {$status}", PHP_EOL;
echo $body;

The proxy value can contain the port, or the port can be supplied separately with CURLOPT_PROXYPORT. Keeping host and port together is convenient for configuration files, while separate fields can be easier to validate in an application form.

Compare the terminal cURL command with PHP

For an HTTP proxy that authorizes your source IP, replace proxy.example:8080 with your endpoint and run the same exit-IP check from the terminal. Use curl.exe if PowerShell resolves curl to an alias.

curl --proxy http://proxy.example:8080 --connect-timeout 10 --max-time 30 --silent --show-error --write-out "\nHTTP %{response_code}\n" "https://api.ipify.org?format=json"

Compare the HTTP status and JSON ip with the PHP result. If they differ, check that both processes use the same endpoint and that the proxy allows the application host’s source IP. Check whether the destination matches NO_PROXY or an explicit CURLOPT_NOPROXY setting. Run curl_version() inside the PHP worker that made the request to compare its libcurl version and TLS backend with curl --version. If PHP reports a certificate error, check the worker’s CA configuration and keep certificate verification enabled.

Authenticated PHP proxy example

<?php
$proxyHost = getenv('PROXY_HOST');
$proxyPort = getenv('PROXY_PORT');
$proxyUser = getenv('PROXY_USERNAME');
$proxyPass = getenv('PROXY_PASSWORD');

$curl = curl_init('https://api.ipify.org?format=json');
curl_setopt_array($curl, [
    CURLOPT_PROXY => "{$proxyHost}:{$proxyPort}",
    CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
    CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
    CURLOPT_PROXYUSERNAME => $proxyUser,
    CURLOPT_PROXYPASSWORD => $proxyPass,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_FOLLOWLOCATION => false,
]);

$body = curl_exec($curl);
$errorNumber = curl_errno($curl);
$connectStatus = curl_getinfo($curl, CURLINFO_HTTP_CONNECTCODE);
$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);

if ($connectStatus === 407 || ($connectStatus === 0 && $status === 407)) {
    throw new RuntimeException('Proxy authentication required (HTTP 407).');
}
if ($body === false) {
    throw new RuntimeException("cURL request failed (error {$errorNumber}).", $errorNumber);
}
if ($status < 200 || $status >= 300) {
    throw new RuntimeException("Unexpected HTTP status: {$status}");
}

echo $body;

Environment variables keep secrets out of source control, but they still need secure deployment permissions. Do not log the full proxy URL or credential option values. Separate username and password options also avoid ambiguous parsing when a value contains punctuation. With a plain http:// proxy, Basic proxy credentials are not protected merely because the destination URL is HTTPS.

BuyProxies title card reading PHP Proxies
Keep proxy configuration separate from application code and prove the route with one harmless request before adding concurrency.

Validate proxy configuration before curl_exec

Treat proxy values as untrusted configuration even when they come from an internal panel. Require a non-empty host, an integer port in the valid range, an allowed proxy type and the complete credential pair when authentication is enabled. Reject line breaks and unexpected URL components before building the cURL options. If users can select destinations, apply an explicit allowlist so the proxy feature does not become a path to internal services or arbitrary network access. With the host and port separated, check the endpoint before passing it to cURL.

At application startup, inspect curl_version() and log non-secret capability information such as libcurl version, SSL backend and supported protocols. A constant can exist in PHP while the linked libcurl build behaves differently across servers. Keep environment-specific differences in deployment diagnostics, not in a page response. Test HTTP, SOCKS and proxy-side DNS only when the installed build and purchased endpoint actually support them.

Protect credentials and diagnostic output

Environment variables are common, but secret managers or protected service configuration provide better rotation and audit controls. Pass credentials into the process at deployment time, restrict who can read the runtime environment and rotate them after exposure. Never include the proxy password, full proxy URL, request Authorization header, cookies or response body in a general error log.

Use a neutral proxy identifier in logs and retain the provider mapping in a restricted store. When verbose cURL output is necessary, direct it to a protected temporary stream, reproduce one request, redact secrets and delete the diagnostic artifact according to the incident process. Production debugging should be short-lived and explicit.

SOCKS5 proxy with PHP cURL

<?php
$curl = curl_init('https://api.ipify.org?format=json');
curl_setopt_array($curl, [
    CURLOPT_PROXY => 'proxy.example:1080',
    CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME,
    CURLOPT_PROXYUSERNAME => 'username',
    CURLOPT_PROXYPASSWORD => 'password',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30,
]);

$body = curl_exec($curl);
if ($body === false) {
    throw new RuntimeException(curl_error($curl), curl_errno($curl));
}

CURLPROXY_SOCKS5_HOSTNAME asks the proxy side to resolve the destination hostname, while CURLPROXY_SOCKS5 can involve local resolution. Availability depends on the libcurl build used by PHP. Confirm supported protocols through curl_version() and test DNS behavior in the deployed environment.

Reusable request function

<?php
function fetchThroughProxy(string $url, array $proxy): array
{
    $curl = curl_init($url);
    $options = [
        CURLOPT_PROXY => $proxy['host'] . ':' . $proxy['port'],
        CURLOPT_PROXYTYPE => $proxy['type'] ?? CURLPROXY_HTTP,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HEADER => false,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_USERAGENT => 'ApprovedMonitor/1.0',
    ];

    if (isset($proxy['username'], $proxy['password'])) {
        $options[CURLOPT_PROXYUSERNAME] = $proxy['username'];
        $options[CURLOPT_PROXYPASSWORD] = $proxy['password'];
    }

    curl_setopt_array($curl, $options);

    $body = curl_exec($curl);
    $info = curl_getinfo($curl);
    $error = $body === false ? curl_error($curl) : null;
    $errorNumber = curl_errno($curl);

    return [
        'ok' => $body !== false && ($info['http_code'] ?? 0) >= 200
            && ($info['http_code'] ?? 0) < 400,
        'status' => (int) ($info['http_code'] ?? 0),
        'connect_status' => curl_getinfo($curl, CURLINFO_HTTP_CONNECTCODE),
        'body' => $body === false ? null : $body,
        'error' => $error,
        'error_number' => $errorNumber,
        'total_time' => (float) ($info['total_time'] ?? 0),
    ];
}

PHP cURL options for proxy type, credentials and timeouts

Option Purpose
CURLOPT_PROXY Proxy hostname or IP, optionally with port
CURLOPT_PROXYPORT Proxy port when it is not included in the proxy value
CURLOPT_PROXYTYPE HTTP, SOCKS4, SOCKS5 or related supported type
CURLOPT_PROXYUSERNAME / CURLOPT_PROXYPASSWORD Separate proxy credential values
CURLOPT_PROXYAUTH Allowed HTTP proxy authentication methods
CURLOPT_NOPROXY Comma-separated hosts that should bypass the proxy
CURLOPT_CONNECTTIMEOUT Maximum time allowed to establish the connection
CURLOPT_TIMEOUT Maximum total request time

PHP version note: curl_close() has been a no-op since PHP 8.0 and is deprecated in PHP 8.5. The examples let the handle leave scope naturally. A legacy PHP 7-only application can still close long-lived handles explicitly.

BuyProxies title card reading PHP Proxies
Separate configuration, connection, destination response and structured error handling so the application reports the correct failure.

Distinguish curl_exec failures from HTTP status codes

For HTTPS through an HTTP proxy, read CURLINFO_HTTP_CONNECTCODE for the proxy reply and CURLINFO_RESPONSE_CODE for the destination reply. A CONNECT 407 can make curl_exec() return false while the destination status is still 0. Check the proxy credentials and authentication method in that case. See the CONNECT 407 example for command-line cURL.

When the transfer completes and returns an HTTP status such as 403 or 429, inspect the destination reply. If the transfer fails, record the CONNECT status, response status and cURL error number before releasing the handle. Use the separate codes to decide whether to check proxy authentication, the connection or the destination response.

Define an application result with a transport flag, cURL error number, HTTP status, total time, proxy identifier and a bounded response sample only when safe. Retry a small set of temporary connection failures with backoff. Do not rotate on destination authentication failures, policy denials or rate limits. For stateful requests, avoid retrying non-idempotent actions unless the application can prove the first attempt did not complete.

Connection reuse, concurrency and time budgets

Reuse a handle or a small controlled pool only when requests share compatible settings, and reset options that can leak across calls. Set a connect timeout and a total timeout; in batch work, also enforce an overall job deadline. A hundred individual 30-second timeouts can still keep a worker busy much longer than the operator expects.

Increase concurrency gradually while measuring success rate and latency percentiles. Limit simultaneous connections per endpoint and per destination, and honor target rate limits. If failures rise with load, return to one request before buying more addresses. The bottleneck may be DNS, the proxy, the destination, local file descriptors or PHP worker capacity.

PHP proxy production checklist

  • Validate destination, host, port, protocol and credential presence before creating the request.
  • Keep TLS peer and host verification enabled and maintain the server CA bundle.
  • Set explicit connect and total timeouts plus a job-level deadline.
  • Record cURL errors separately from HTTP status responses and target error bodies.
  • Retry only classified temporary failures with a small limit and backoff.
  • Keep passwords, cookies, tokens and full proxy URLs out of logs and exceptions.
  • Test one endpoint and the real permitted target before adding rotation or concurrency.
  • Monitor success rate, response time and error categories by neutral proxy identifier.

Run the checklist in a staging environment with placeholder credentials first, then repeat one harmless production request using the real secret source. Add an automated smoke test that verifies configuration can be loaded without printing it, the proxy route changes the public IP, and an expected destination status is returned. Alert on error categories and sustained latency changes rather than on a single slow response. Review retry and timeout values after observing real traffic; copied defaults rarely match every workload. When the production API requires an exclusive allowlisted address, use the dedicated proxy assigned to that integration.

Proxy lists and rotation

Rotation should be a deliberate application rule, not an automatic retry for every error. Classify failures first. A timeout may justify trying another healthy endpoint; an HTTP 401 means destination authentication failed; HTTP 407 normally points to proxy authentication; HTTP 429 indicates the destination is asking the client to slow down. Retrying those responses blindly through more IPs can make the workflow less reliable and violate target rules.

$proxy = $healthyProxies[array_rand($healthyProxies)];
$result = fetchThroughProxy($url, $proxy);

if (!$result['ok']) {
    error_log(json_encode([
        'status' => $result['status'],
        'connect_status' => $result['connect_status'],
        'error_number' => $result['error_number'],
        'total_time' => $result['total_time'],
    ]));
}

Do not include credentials in logs. Maintain health state separately and place limits on retries, total time and concurrency.

Common PHP proxy errors

  • Could not resolve proxy: check the hostname and server DNS.
  • Connection timed out: verify port, firewall, route and endpoint availability.
  • HTTP 407: proxy authentication is missing, rejected or uses an unsupported method.
  • SSL certificate problem: repair the CA bundle; do not disable peer verification as a permanent workaround.
  • Empty response: inspect curl_errno(), curl_error() and HTTP status separately.

PHP cURL proxy FAQ

What value goes in CURLOPT_PROXY?

Use the proxy hostname or IP, optionally followed by a colon and port. Keep credentials in the proxy credential options rather than placing secrets in the URL.

How do I set proxy authentication?

Prefer separate CURLOPT_PROXYUSERNAME and CURLOPT_PROXYPASSWORD values, and set CURLOPT_PROXYAUTH when a particular HTTP proxy authentication method is required.

Should I disable SSL verification?

No. Keep peer and host verification enabled. Update the CA bundle or diagnose an authorized inspection proxy when verification fails.

Can PHP use SOCKS5?

Yes, when the installed libcurl supports it. Set the appropriate CURLPROXY_SOCKS5 or CURLPROXY_SOCKS5_HOSTNAME type and test DNS behavior.

When should PHP retry through another proxy?

Only after classifying a temporary transport failure and confirming that retrying the operation is safe. Do not rotate blindly on authentication, policy or rate-limit responses.

Test before adding PHP concurrency

Verify one endpoint and one real destination, then add structured errors, bounded retries and modest concurrency. The official PHP manual documents curl_setopt() and the available cURL constants. For command-line examples, see our cURL proxy examples.

JavaScript developers can compare these PHP patterns with the Node.js proxy guide for fetch, Axios, Undici and environment variables.

Which settings should I check in PHP?

Record the proxy CONNECT status, destination HTTP status and cURL error number separately. Check proxy authentication when the CONNECT response is 407. Keep proxy credentials out of destination headers, set connection and total time limits, and leave TLS verification enabled. A proxy 407 and a destination 403 need different fixes; consult the PHP cURL execution reference before treating an HTTP error as a transport failure. For a DNS or route symptom, choose the relevant lookup from the network diagnostic tools and save it alongside the cURL error.

Before you start What to confirm
Proxy type Dedicated for important workflows; semi-dedicated for lower-risk testing.
Protocol Use HTTP/HTTPS unless the tool clearly supports SOCKS5.
Authentication Check username/password format or IP whitelist before blaming the proxy.
Quality control Run a tester check, then verify location and speed.
Scroll to Top