PHP: Using cURL with TTProxy

Azura Liu

This is a guide on how to use TTProxy with PHP’s cURL functions. In this tutorial, we will send our HTTP request via a specific proxy IP and port.

Why use a proxy?

There are various reasons why you might want to use a proxy with cURL:

  1. To get around regional filters and country blocks.
  2. Using a proxy IP allows you to mask your own IP address.
  3. To debug network connection issues.

Using a proxy with PHP’s cURL functions.

Take a look at the following PHP code, which you can use to authenticate with a proxy via cURL and send a HTTP GET request.

$license = 'your license';
$secret = 'your license's secret';
$time = time();

// Step 1 : Obtain proxy IP   
// Important: the ip addresses in the obtained ip:port list belong to TTProxy central server, NOT the proxy node ip which finally communicate with the target server.   
$queries = [
    'license' => $license,
    'time' => $time,
    'cnt' => 1, // Get number of proxies (optional)

$queries['sign'] = md5($license . $time . $secret);

$getProxyApiurl = '' . http_build_query($queries);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $getProxyApiurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

if (!$response) {
  die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;


$resData = json_decode($response, true);

// Step 2 : Use proxy IP    
$targetUrl = "";

$proxyServer = $resData['data']['proxies'][0];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetUrl);

curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);


In the code snippet above,we obtain a proxy IP from api — see Step 1 : Obtain proxy IP in code, and then connected to a proxy that requires authentication before sending a simple GET request — see the Step 2 : Use proxy IP in code.

If the proxy in question does not require authentication, then you can omit the CURLOPT_PROXYUSERPWD line from your code.

“Failed to connect to port 1129: Timed out”

This means that cURL could not connect to the proxy on that IP and port. Make sure that both the IP and port are correct and that the proxy is operating correctly.

“Failed to connect to port 1129: Connection refused”

This error usually occurs when you have specified an incorrect port number. i.e. The IP address of the proxy was correct, but it is not listening for requests on that port. There is also the possibility that the server is up, but the software that runs the proxy is not running.

“Received HTTP code 407 from proxy after CONNECT”

The username and password combination that you are using with CURLOPT_PROXYUSERPWD is incorrect. Make sure that you are separating the username and password by a colon : character.