cURL Proxy Examples: HTTP, SOCKS5 and Authentication

To use a proxy with cURL, pass the endpoint to --proxy or -x and choose the scheme supported by that endpoint. The examples below cover HTTP and HTTPS destinations, proxy authentication, environment settings and bounded tests without disabling TLS verification.

These examples focus on command-line cURL. They are useful for testing a proxy endpoint before using it in scripts, monitoring jobs, browser profiles or application code.

How to use a proxy with curl

Use --proxy for the endpoint and --proxy-user for credentials. cURL resolves the destination locally with socks5://. With socks5h://, the proxy resolves it. Test one endpoint with the Proxy Tester, then reproduce the same route in cURL.
Jump to an example · HTTP · HTTPS · SOCKS5 · PHP

Should the curl proxy URL start with socks5 or socks5h? Use socks5h://HOST:PORT to have the proxy look up the destination name. Use socks5://HOST:PORT when the lookup should happen on your machine. Keep the same destination and proxy when comparing the two, so the resolver is the only setting you change.

Send a cURL request through an HTTP proxy

curl --proxy http://proxy.example.com:8080 https://example.com/

Using PHP? See the CURLOPT_PROXY example for PHP.

This tells cURL to connect to the proxy first, then request the HTTPS target through that route. Replace the host and port with the endpoint assigned in your proxy panel.

Connect to an HTTPS proxy endpoint with cURL

curl --proxy https://proxy.example.com:443 https://example.com/

Use this form when your provider supplies a TLS-enabled HTTPS proxy endpoint. Here the first https:// starts TLS to the proxy and the second protects the request to the destination. Use your assigned host and port and keep certificate verification enabled for both connections.

Set a proxy username and password in cURL

curl --proxy http://proxy.example.com:8080 \
  --proxy-user "username:password" \
  https://example.com/

Use --proxy-user instead of embedding credentials inside the URL when possible. It is easier to read, safer for copy/paste and less likely to break when the password contains reserved URL characters. If authentication keeps failing, review the proxy authentication guide.

SOCKS5 and SOCKS5h examples

curl --proxy socks5://proxy.example.com:1080 https://example.com/
curl --proxy socks5h://proxy.example.com:1080 https://example.com/

With cURL, socks5:// resolves the destination hostname locally. socks5h:// asks the SOCKS proxy to resolve the hostname remotely. That difference matters when DNS location, privacy or target reachability is being tested.

With socks5, cURL resolves the destination on your device and sends its IP. With socks5h, cURL sends the hostname and the proxy resolves it.
The scheme changes who resolves the destination hostname. Both examples use the same SOCKS proxy.

The equivalent long option is --socks5-hostname. It also sends the destination hostname to the proxy for resolution:

curl --socks5-hostname proxy.example.com:1080 \
  --proxy-user "username:password" \
  https://example.com/

cURL proxy environment variables

Environment variables are useful for temporary shell sessions and tools that invoke cURL without repeating proxy flags. cURL recognizes protocol-specific variables, a general fallback and an exclusion list:

export http_proxy="http://proxy.example.com:8080"
export HTTPS_PROXY="http://proxy.example.com:8080"
export ALL_PROXY="socks5h://proxy.example.com:1080"
export NO_PROXY="localhost,127.0.0.1,.example.test"

curl https://example.com/

Use lowercase http_proxy; cURL deliberately treats that name differently from most uppercase proxy variables. An explicit --proxy selects the endpoint for that command. A matching NO_PROXY entry can still bypass it. On a POSIX shell, add --noproxy "" when you need this one test to go through the proxy regardless of that list. Keep credentials out of shared shell profiles and CI logs. The official cURL tutorial documents the supported variables and precedence.

Use a config file for repeatable tests

# curl-proxy.conf
proxy = "http://proxy.example.com:8080"
proxy-user = "username:password"
connect-timeout = 10
max-time = 30
location

curl --config curl-proxy.conf https://example.com/

A config file reduces copy mistakes and keeps options consistent across checks. Protect the file permissions if it stores proxy credentials, and do not commit it to a public repository.

PHP cURL proxy example

PHP’s cURL extension exposes the same libcurl proxy controls. Set the endpoint with CURLOPT_PROXY, select the protocol with CURLOPT_PROXYTYPE and keep authentication in CURLOPT_PROXYUSERPWD:

<?php
$handle = curl_init('https://example.com/');

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

$response = curl_exec($handle);
$exitCode = curl_errno($handle);
$tunnelCode = curl_getinfo($handle, CURLINFO_HTTP_CONNECTCODE);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);

if ($response === false || $httpCode >= 400) {
    throw new RuntimeException(
        "cURL $exitCode, CONNECT $tunnelCode, destination HTTP $httpCode"
    );
}

For SOCKS5 with proxy-side DNS, change CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME and use the assigned SOCKS5 host and port. Use CURLPROXY_SOCKS5 when the PHP host should resolve the destination. A SOCKS5 request has no HTTP CONNECT response. See the official references for CURLOPT_PROXY and CURLOPT_PROXYTYPE.

Timeout and diagnostic flags

curl --proxy http://proxy.example.com:8080 \
  --connect-timeout 10 \
  --max-time 30 \
  --verbose \
  https://example.com/

--connect-timeout limits connection setup, including DNS and the required handshakes. --max-time limits the full transfer. --verbose is useful for controlled troubleshooting, but it can expose hostnames and authentication flow details in logs.

Common cURL proxy errors

Symptom Likely cause Fix
407 Proxy Authentication Required Credentials are missing or wrong. Check username, password, protocol and port.
Connection timeout The proxy host or port is unreachable from the current network. Test the endpoint separately and compare firewall/network rules.
Target returns a block page The proxy works, but the destination has its own policy or reputation decision. Record status, headers and target response before changing endpoints.
DNS result differs from browser Local DNS and proxy-side DNS are being mixed. Compare socks5:// and socks5h:// behavior.

For a failed tunnel, use the cURL proxy diagnostic commands to compare the CONNECT result with the destination HTTP status.

Related setup guides

For a private endpoint, compare BuyProxies HTTP and SOCKS5 plans and test the exact destination before scaling.

Scroll to Top