Skip to main content
The Center's Directory
Code examples

Use a proxy with PHP cURL

Complete proxy requests using PHP cURL extensions, accounting environment variables, and connection timeouts.

Maintained by the ZZHTTP engineering teamUpdated September 20, 2026

PHP services can use cURL to extend the configuration of proxy hosts, ports, and authentication, without having to stack credentials into the target URL.

Precondition

  • The PHP runtime has enabled the cURL extension.
  • The account is closed. us.zzhttp.com:7878; Injection through controlled environment PROXY_USERNAME and PROXY_PASSWORD.
  • The production environment shuts down the detailed debug output containing sensitive request information.

Complete example

PHP
<?php

$handle = curl_init('https://ipinfo.io/json');
curl_setopt_array($handle, [
    CURLOPT_PROXY => 'us.zzhttp.com',
    CURLOPT_PROXYPORT => 7878,
    CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
    CURLOPT_PROXYUSERPWD => sprintf(
        '%s:%s',
        getenv('PROXY_USERNAME'),
        getenv('PROXY_PASSWORD')
    ),
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_RETURNTRANSFER => true,
]);

$body = curl_exec($handle);
if ($body === false) {
    $message = curl_error($handle);
    curl_close($handle);
    throw new RuntimeException($message);
}

$status = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
curl_close($handle);
if ($status >= 400) {
    throw new RuntimeException("HTTP {$status}");
}

echo $body;

Expected results

  • cURL uses an HTTP proxy to access the HTTPS target and return the export information.
  • The connection waits for up to 10 seconds and the entire request waits for up to 30 seconds.
  • The business code checks for transmission errors and HTTP status at the same time.

Error handling

  • 407 Priority Check CURLOPT_PROXYUSERPWD The corresponding accounts are complete.
  • Connection timeout first checks the proxy address, port, and server exit network.
  • TLS errors should not be masked by turning off certificate validation.

Safety tips

Do not enable public debug logs that record authentication headers. The proxy URL, proxy username, and password are removed before exception reporting, leaving only the cURL error code and desensitized context.

Did this answer your question?

Feedback is used only to improve the public documentation and does not submit accounts or request information.