Skip to content
Hopmind

Guides

How to send requests through a proxy in PHP (cURL)

A request through an HTTP proxy and SOCKS5 with a login and password in PHP using the cURL extension.

What you need

The proxy is issued in the format host:port:login:password. Replace HOST, PORT, LOGIN and PASSWORD with your own values. You need PHP with the cURL extension: check with the command php -m, the list should contain the line curl.

Example

<?php

$ch = curl_init("https://api.ipify.org");

curl_setopt_array($ch, [
    // Return the response as a string instead of printing it immediately
    CURLOPT_RETURNTRANSFER => true,
    // The proxy address as host:port
    CURLOPT_PROXY => "HOST:PORT",
    // HTTP proxy (the default value); it works for HTTPS sites too
    CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
    // Login and password separated by a colon; special characters need no encoding
    CURLOPT_PROXYUSERPWD => "LOGIN:PASSWORD",
    // Timeouts: for the connection and for the whole request
    CURLOPT_CONNECTTIMEOUT => 15,
    CURLOPT_TIMEOUT => 30,
]);

$response = curl_exec($ch);

if ($response === false) {
    echo "cURL error: " . curl_error($ch) . PHP_EOL;
} else {
    // The service returns the address the request came from
    echo $response . PHP_EOL;
}

curl_close($ch);

If SOCKS5

For SOCKS5, specify the separate port and the CURLPROXY_SOCKS5_HOSTNAME type: in that case the proxy resolves the site name. The CURLPROXY_SOCKS5 type resolves names on your side.

<?php

$ch = curl_init("https://api.ipify.org");

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_PROXY => "HOST:SOCKS_PORT",
    CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME,
    CURLOPT_PROXYUSERPWD => "LOGIN:PASSWORD",
    CURLOPT_CONNECTTIMEOUT => 15,
    CURLOPT_TIMEOUT => 30,
]);

echo curl_exec($ch) . PHP_EOL;
curl_close($ch);

Common errors

  1. Proxy response code 407 (curl_error shows text like Received HTTP code 407 from proxy after CONNECT): the login or password is wrong. Check the order in the issued host:port:login:password.
  2. Operation timed out or Connection timed out: check HOST and PORT, whether the SOCKS5 port was put into HTTP mode, and increase CURLOPT_TIMEOUT.
  3. Special characters in the password: in CURLOPT_PROXYUSERPWD the password is set as is and needs no encoding. A colon is allowed in the password but not in the login. If you write the details directly in the CURLOPT_PROXY address, encode them with rawurlencode().
  4. The https:// scheme in CURLOPT_PROXY: the issued proxy works over HTTP, so use HOST:PORT without a scheme or http://HOST:PORT, even for HTTPS sites.
  5. Call to undefined function curl_init(): the cURL extension is not enabled in php.ini.

Sources

Checked against the official documentation: October 2026. Program interfaces change: if item names differ, follow the documentation.

All guides are collected on the Guides page.

Find a proxy in the chat