Black Friday Up to 20% off with code BLACKFRIDAY until Tue 1 Dec, 23:59 UK time
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 within 48 hours of your report, or the unused part refunded. A website blocking the proxies isn’t covered. Refund Policy

Quick answer

To use a proxy with PHP, follow four steps: (1) Set CURLOPT_PROXY to http://HOST:PORT; (2) Set CURLOPT_PROXYUSERPWD to USERNAME:PASSWORD; (3) With Guzzle, pass "proxy" => url in the request options instead; (4) Use socks5h:// for SOCKS5. Take the host, port, username and password from a line in your ZenProxy generator.

You need PHP and a ZenProxy proxy line from the generator. In the examples, replace HOST, PORT, USERNAME and PASSWORD for the values in your line (where to find them).

Quick setup

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

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;
}

Your proxy details

After you buy a plan, open My proxies. Choose the Proxy Type (Rotating or Sticky; static IP plans skip this and show your list of IPs), the Protocol (HTTP, or SOCKS5 where your plan offers it) and a Format, then copy a line. The generator offers three formats (SOCKS5 lines always use the protocol:// format):

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). Static Datacenter, Shared ISP and Private ISP plans give you a fixed list of IPs, one per line as IP:PORT:USERNAME:PASSWORD: Private ISP IPs are yours alone, while Shared ISP IPs can also be used by other customers. Rotating Datacenter and IPv6 plans give you one endpoint, with rotating or sticky lines; IPv6 only reaches sites that have an IPv6 address.

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.

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

Build the line in the generator: it writes the country, and a state, a city or a sticky session if you want one, into the username in the format your plan uses, and shows only the options your plan supports. On most residential plans the country is -country- and a two-letter code at the end of the username (for example USERNAME-country-gb), but older Unlimited Residential plans use a different format, so do not edit that username by hand. 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 within 48 hours of your report, or the unused part refunded. A website blocking the proxies isn’t covered. Refund Policy

Related guides

All integrations →