Developers

Integrations

Guide

PHP

Send requests from PHP through a Crawlhub proxy with the cURL extension.

Your Crawlhub connection

FieldValue
GatewayUse the assigned host and port for your client’s protocol.
Residential usernameCopy the username from your configured residential connection.
ISP / Datacenter usernameUse the username and port assigned to your product.

Scroll horizontally to read all columns.

Use the protocol supported by this client. The examples below use PROXY_SERVER or separate host/port fields for these connection values. Read the proxy parameter reference.

Prepare the client

You need PHP 8 or newer with the cURL extension. Run php -m and look for curl in the list.

Save the example as exit-ip.php and start it with php exit-ip.php.

Put your proxy login in environment variables, not in the code. Copy the username and password from your dashboard.

Set your proxy login
export PROXY_USERNAME="your-proxy-username"
export PROXY_PASSWORD="your-proxy-password"

Connection example

The example connects to http://proxy.crawlhub.ai:8000 and prints the exit IP that the target sees.

PHP proxy example
<?php
$user = getenv('PROXY_USERNAME');
$pass = getenv('PROXY_PASSWORD');

$ch = curl_init('https://api.ipify.org');
curl_setopt_array($ch, [
    CURLOPT_PROXY => 'http://proxy.crawlhub.ai:8000',
    CURLOPT_PROXYUSERPWD => $user . ':' . $pass,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30,
]);

$ip = curl_exec($ch);
if ($ip === false) {
    fwrite(STDERR, 'Request failed: ' . curl_error($ch) . PHP_EOL);
    exit(1);
}
echo 'Exit IP: ' . $ip . PHP_EOL;

Add a country or session

Add options to the end of the username. The password stays the same. __cc_us picks a US exit. __cc_us__sid_job01 picks a US exit and keeps the same IP while you reuse that session ID.

Username options
// US exit
$user = getenv('PROXY_USERNAME') . '__cc_us';

// Or a US exit that keeps the same IP for session job01
// $user = getenv('PROXY_USERNAME') . '__cc_us__sid_job01';

Put the country before the session. Read the proxy parameter reference for all options.

Reuse the client

Reuse one cURL handle for requests with the same proxy setting. cURL then keeps the connection to the proxy open between requests.

For a second country or session, change CURLOPT_PROXYUSERPWD on the handle or use a second handle.

Verify the connection

Run the example. It prints the exit IP, which is the address of the proxy, not your own. Run it again without a session ID and the IP can change.

Use a public HTTPS target on port 443. The gateway also forwards plain http:// on port 80. It refuses other ports and private or local addresses. Keep TLS verification on.

When it fails

407 means the proxy did not accept the login. Check the username, the password and the options you added. curl_exec returns false and curl_error says the CONNECT tunnel failed with response 407. A 407 can also mean that the trial ended or no traffic is left, so check your dashboard.

403 means the gateway refused the target, for example a port other than 80 or 443. 502 means the exit or the target did not answer. Retry after a short wait.

See Troubleshooting for other errors. Send your client version and the error text, without your password, to support.

Search documentation

Search guides, products and reference pages.