Programming languages

How to use a proxy in PHP

PHP powers many price trackers, SEO tools and WordPress plugins that fetch remote pages. Almost every PHP HTTP client sits on the cURL extension, so the proxy comes down to a few cURL options: the address, the type and the username with your token.

The examples use the cURL extension directly, then Guzzle and Laravel, which accept the proxy as a single URL. Everything works on PHP 8.1 and later.

Updated 23 November 2026

What you need
Hostgate.firstproxy.shop
HTTP / HTTPS port8000
SOCKS5 port9000
Usernameuser-country-de
PasswordYOUR_TOKEN

PHP cURL supports HTTP on port 8000 and SOCKS5 on port 9000, both with username and password. Your username and token appear on the Proxy setup page of your dashboard after your first order.

Setup

How do I connect PHP to a proxy?

Follow the steps in order. Replace YOUR_TOKEN with the token from your dashboard and change the country, city or session in the username to suit the job.

  1. Check that the cURL extension is enabled

    Run php -m and look for curl. On Debian and Ubuntu install it with the php-curl package, then restart PHP-FPM or your web server.

    php -m | grep curl
    # Debian or Ubuntu
    sudo apt install php-curl
  2. Send a request through the HTTP proxy

    Set the gateway in CURLOPT_PROXY and the credentials in CURLOPT_PROXYUSERPWD. HTTPS targets are tunnelled automatically, so CURLOPT_HTTPPROXYTUNNEL is not needed.

    <?php
    $ch = curl_init('https://ipinfo.io/json');
    curl_setopt_array($ch, [
        CURLOPT_PROXY          => 'gate.firstproxy.shop:8000',
        CURLOPT_PROXYTYPE      => CURLPROXY_HTTP,
        CURLOPT_PROXYUSERPWD   => 'user-country-de:YOUR_TOKEN',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT        => 30,
    ]);
    
    $body = curl_exec($ch);
    if ($body === false) {
        echo 'cURL error ' . curl_errno($ch) . ': ' . curl_error($ch);
    } else {
        echo curl_getinfo($ch, CURLINFO_HTTP_CODE) . ' ' . $body;
    }

    Since PHP 8.0 cURL handles are objects that are freed automatically, so curl_close is not required.

  3. Choose a country, city and sticky session

    Build the username in a function so each job can pass its own targeting. Use the same session name for every request of a flow to keep the IP.

    <?php
    function proxyUser(string $country, ?string $city = null, ?string $session = null, int $ttl = 10): string
    {
        $user = 'user-country-' . $country;
        if ($city) {
            $user .= '-city-' . $city;
        }
        if ($session) {
            $user .= '-session-' . $session . '-ttl-' . $ttl . 'm';
        }
        return $user;
    }
    
    $session = bin2hex(random_bytes(4));
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, proxyUser('de', 'hamburg', $session, 30) . ':YOUR_TOKEN');
  4. Use SOCKS5 with remote DNS

    Switch the type to CURLPROXY_SOCKS5_HOSTNAME and the port to 9000. The hostname variant lets the proxy resolve DNS, unlike CURLPROXY_SOCKS5.

    curl_setopt_array($ch, [
        CURLOPT_PROXY        => 'gate.firstproxy.shop:9000',
        CURLOPT_PROXYTYPE    => CURLPROXY_SOCKS5_HOSTNAME,
        CURLOPT_PROXYUSERPWD => 'user-country-de:YOUR_TOKEN',
    ]);
  5. Use Guzzle or the Laravel HTTP client

    Both take the proxy as one URL in the proxy request option. Guzzle also accepts an array with separate http, https and no keys when some hosts must go direct.

    <?php
    // composer require guzzlehttp/guzzle
    require 'vendor/autoload.php';
    
    use GuzzleHttp\Client;
    
    $client = new Client(['timeout' => 30]);
    $res = $client->request('GET', 'https://ipinfo.io/json', [
        'proxy' => 'http://user-country-de:YOUR_TOKEN@gate.firstproxy.shop:8000',
    ]);
    echo $res->getStatusCode() . ' ' . $res->getBody();
Test

How do I check that the proxy works?

Run the script from step 2 on the command line with php check.php. The JSON should show the country from the username.

A working proxy returns a response like the one shown, trimmed to the main fields. The IP belongs to the exit, the city and country follow your username, and the time zone is where that IP is located, here Germany.

php check.php
ipinfo.io/json response
{
  "ip": "203.0.113.24",
  "city": "Berlin",
  "country": "DE",
  "timezone": "Europe/Berlin"
}

203.0.113.24 is a documentation address. Your result shows a real exit IP.

Targeting

How do I set country, city and session in the username?

All targeting is written into the username, so PHP needs no extra settings to change location. Parts are joined with hyphens in this order after your proxy user. The password is always your token.

Proxy username parts
PartFormatExampleRule
Country-country-{cc}-country-deRequired. Two-letter country code in lowercase. 64 countries are available.
City-city-{city}-city-berlinOptional. City name in lowercase, spaces become hyphens. 172 cities are available.
Session-session-{id}-session-a1b2c3Optional. Any letters and digits you choose. The same id keeps the same IP, a new id gets a new IP.
Session length-ttl-{minutes}m-ttl-30mUsed with a session. Residential 1 to 120 minutes, mobile 1 to 60, long-acting ISP up to 720 (12 hours).
Full usernameuser-country-de-city-berlin-session-a1b2c3-ttl-30m

Find country codes and city values on the proxy locations page.

Troubleshooting

PHP proxy errors and fixes

The problems people run into most often with PHP, with the message you are likely to see and how to solve it.

cURL error 56: Received HTTP code 407 from proxy after CONNECT

The gateway rejected the credentials. Check CURLOPT_PROXYUSERPWD for a missing colon or extra spaces. In a Guzzle URL, wrap the token in rawurlencode() if it contains special characters.

cURL error 28: Operation timed out

Set both CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT, and keep them below the PHP max_execution_time so the script can retry instead of being killed.

cURL error 7: Failed to connect

Outbound ports 8000 or 9000 are blocked. Shared hosting often allows only ports 80 and 443 out. Ask the host to open the port or run the job from a VPS.

The IP is my server address

Another option overrode the proxy, or the request was made with file_get_contents on a different code path. Search the code base for every HTTP call and send all of them through the same client.

Parallel requests share one IP

Handles inside curl_multi that use the same session name share the IP by design. Use a different session per handle, or no session at all, when each request needs its own exit.

FAQ

PHP proxy questions

Short answers to what people ask most about using a proxy with PHP.

How do I use a proxy with PHP cURL?

Set CURLOPT_PROXY to gate.firstproxy.shop:8000, CURLOPT_PROXYTYPE to CURLPROXY_HTTP and CURLOPT_PROXYUSERPWD to "username:YOUR_TOKEN". HTTPS sites are tunnelled through the proxy automatically.

How do I use a SOCKS5 proxy in PHP?

Use port 9000 and set CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME so the proxy resolves hostnames. Credentials go in CURLOPT_PROXYUSERPWD as with HTTP.

How do I set a proxy in Guzzle?

Pass the proxy request option with a URL such as http://username:YOUR_TOKEN@host:port, either per request or in the client defaults.

Can I use a proxy with file_get_contents?

Stream contexts have a proxy option, but credentials must be sent as a manual header and HTTPS handling is limited. The cURL extension or Guzzle is the more reliable choice.

Get your token and run the first PHP request

Create an account, top up from $40 with crypto, and copy the gateway, username and token from the dashboard.