PHP

Send PHP requests through NyxonProxies with the cURL extension and Guzzle: proxy credentials, SOCKS5 with remote DNS, rotation, sticky sessions and errors.

PHP has two common ways to make HTTP requests: the cURL extension and the Guzzle library, which uses cURL underneath. Both take an authenticated proxy in a few options.

cURL extension

php
<?php
$ch = curl_init("https://api.ipify.org");
curl_setopt_array($ch, [
    CURLOPT_PROXY => "http://geo.gw.nyxon.sh:8080",
    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;
}

The proxy address starts with http:// even for HTTPS sites. cURL opens a tunnel through the gateway and runs TLS to the site inside it.

Country and sticky session

Targeting and sessions go in the username:

php
<?php
$sid = bin2hex(random_bytes(4));
$user = "USERNAME-country-de-session-{$sid}-time-10";

foreach ([1, 2] as $i) {
    $ch = curl_init("https://api.ipify.org");
    curl_setopt_array($ch, [
        CURLOPT_PROXY => "http://geo.gw.nyxon.sh:8080",
        CURLOPT_PROXYUSERPWD => "{$user}:PASSWORD",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 30,
    ]);
    echo curl_exec($ch) . PHP_EOL;
}

Both requests print the same German IP for up to 10 minutes. Without the session part, each new handle gets a new IP. A handle you reuse keeps its connection open, so its requests share one IP. See Rotating and sticky sessions.

SOCKS5

Point cURL at the SOCKS5 port with socks5h://, so the gateway resolves the hostname:

php
<?php
$ch = curl_init("https://api.ipify.org");
curl_setopt_array($ch, [
    CURLOPT_PROXY => "socks5h://geo.gw.nyxon.sh:1080",
    CURLOPT_PROXYUSERPWD => "USERNAME:PASSWORD",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
]);
echo curl_exec($ch) . PHP_EOL;

Guzzle

bash
composer require guzzlehttp/guzzle
php
<?php
require "vendor/autoload.php";

use GuzzleHttp\Client;

$client = new Client(["timeout" => 30]);
$response = $client->get("https://api.ipify.org", [
    "proxy" => "http://USERNAME:[email protected]:8080",
]);
echo $response->getBody() . PHP_EOL;

The proxy option also takes socks5h:// addresses. Percent-encode special characters in the password with rawurlencode() before you put it in the URL.

Errors

curl_error() returns the same messages as the cURL command line:

MessageCause
CONNECT tunnel failed, response 407Wrong username or password.
Failed to connect to ... port ...Wrong host or port, or a firewall.
Could not resolve proxyA typo in the gateway hostname.
An SSL certificate error with unable to get local issuer certificatePHP has no CA bundle configured, which is common on Windows. Point curl.cainfo in php.ini to a CA bundle file. The proxy does not cause this.

Guzzle raises a GuzzleHttp\Exception\ConnectException with the same message. The full list is in Error codes.

Last updated

Still stuck?

People answer the live chat around the clock. Send the proxy line you used (without the password) and the error you see.