New Residential Premium, our top-tier pool for the toughest sites
PH

Scraping & code · Setup guide

How to use a proxy in PHP (cURL and Guzzle)

PHP’s cURL extension and Guzzle both take a proxy with credentials. Examples for HTTP and SOCKS5, rotating and sticky IPs, and country targeting.

Broken on our side? Fixed in 48h or refunded.

Quick setup

  1. 1Set CURLOPT_PROXY to http://HOST:PORT.
  2. 2Set CURLOPT_PROXYUSERPWD to USERNAME:PASSWORD.
  3. 3Or pass "proxy" => url in Guzzle.
  4. 4Use socks5h:// for SOCKS5.

Your proxy details

After you buy a plan, open My proxies. Choose the Proxy Type (Rotating or Sticky), the Protocol (HTTP or SOCKS5) and a Format, then copy a line. The generator offers three formats:

HOST:PORT:USERNAME:PASSWORD

Paste into apps and anti-detect browsers that split the fields or auto-fill them.

USERNAME:PASSWORD@HOST:PORT

Tools that want credentials before the address.

http://USERNAME:PASSWORD@HOST:PORT

Code and command-line tools. This is the proxy URL used in the examples below.

In the examples on this page, replace USERNAME, PASSWORD, HOST and PORT with the values from your line. SOCKS5_PORT is the port the generator shows when you pick SOCKS5.

Residential lines are either rotating (a new IP on new connections) or sticky (one IP for the time you choose). Datacenter and ISP plans give you a list of dedicated IPs, one per line as IP:PORT:USERNAME:PASSWORD, and each line is one fixed IP.

To test a line, open https://ipinfo.io/json through it: it shows the exit IP and country. For HTTP lines you can also use the free proxy checker.

PHP cURL

proxy.php
<?php
$ch = curl_init('https://ipinfo.io/json');
curl_setopt_array($ch, [
    CURLOPT_PROXY          => 'http://HOST:PORT',
    CURLOPT_PROXYUSERPWD   => 'USERNAME:PASSWORD',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 30,
]);

$body = curl_exec($ch);
if ($body === false) {
    echo 'Error: ' . curl_error($ch) . PHP_EOL;
} else {
    echo $body . PHP_EOL;
}

SOCKS5 with cURL

PHP
curl_setopt_array($ch, [
    CURLOPT_PROXY        => 'socks5h://HOST:SOCKS5_PORT',
    CURLOPT_PROXYUSERPWD => 'USERNAME:PASSWORD',
]);

socks5h:// makes the proxy resolve hostnames. Setting CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME does the same.

Guzzle

Install
composer require guzzlehttp/guzzle
guzzle.php
<?php
require 'vendor/autoload.php';

$client = new GuzzleHttp\Client(['timeout' => 30]);

$response = $client->get('https://ipinfo.io/json', [
    'proxy' => 'http://USERNAME:PASSWORD@HOST:PORT',
]);
echo $response->getBody() . PHP_EOL;

proxy also takes an array such as ['http' => $url, 'https' => $url, 'no' => ['localhost']] for different settings per scheme.

Rotating and sticky IPs

Rotation happens per connection. A rotating line gets a new IP when your client opens a new connection; requests sent through the same open HTTPS tunnel leave from the same IP. A new cURL handle opens a new connection, so a rotating line gives each new handle a new IP. Reusing one handle, or one Guzzle client, can keep the connection and its IP.

To make sticky lines, open the generator, set Proxy Type to Sticky, choose a Sticky Duration if your plan offers one, set how many lines you want in Sticky Amount, then copy or download them. Each line keeps its IP for the chosen time. The session part of a sticky username differs by plan, so always generate sticky lines instead of editing usernames by hand.

Target a country

On residential plans, add -country- and a two-letter country code to the end of the username, for example USERNAME-country-us or USERNAME-country-gb. To combine a country with a state, a city or a sticky session, build the line in the generator, which shows only the options your plan supports. Datacenter and ISP IPs have a fixed location that you choose when you buy them.

PHP
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'USERNAME-country-us:PASSWORD');

Troubleshooting

  • CONNECT tunnel failed, response 407 (or “Received HTTP code 407 from proxy after CONNECT” on older libcurl)

    The proxy rejected the credentials. Check CURLOPT_PROXYUSERPWD.More on this error →

  • Failed to connect to HOST port PORT

    Wrong host or port, or your server’s firewall blocks outgoing connections to that port.More on this error →

  • SSL errors after adding the proxy

    Keep http:// in CURLOPT_PROXY; HTTPS sites are still encrypted inside the tunnel.More on this error →

  • Timeouts

    Set an explicit timeout of 30–60 seconds, retry on a new connection, and lower concurrency.More on this error →

Seeing another error? Read proxy errors and how to fix them, or contact support.

Questions

How do I add proxy authentication to PHP cURL?

Set CURLOPT_PROXY to http://HOST:PORT and CURLOPT_PROXYUSERPWD to USERNAME:PASSWORD, or put both in one URL in CURLOPT_PROXY.

How do I use a SOCKS5 proxy in PHP?

Set CURLOPT_PROXY to socks5h://HOST:SOCKS5_PORT and the credentials in CURLOPT_PROXYUSERPWD.

How do I set a proxy in Guzzle?

Pass "proxy" => "http://USERNAME:PASSWORD@HOST:PORT" in the request options or the client config.

How do I rotate IPs in PHP?

Use a rotating line and a new cURL handle per request. For a stable IP, use a sticky line.

Try it with PHP for $0.49

Get 1 GB of Residential Lite, then copy your rotating or sticky lines from the generator and paste them into the steps above.

Broken on our side? Fixed in 48h or refunded.

Related guides

All integrations →