PHP cURL Requests with Tor Proxy

In this tutorial, we are going to see the method to connect Tor Proxy in PHP cURL Request.

Requirements

  • Tor Proxy Must Enabled on your System or Server
  • Latest and Updated Version of PHP
  • PHP cURL Module

Connect Tor Proxy

  • Test and Connect Tor Proxy Connection
<?php

$ip = '127.0.0.1';
$port = '9050';
$torSocks5Proxy = 'socks5://127.0.0.1:9050';

$fp = fsockopen($ip, $port, $error_number, $err_string, 10);
if (!$fp) {
echo json_encode('Error while changing Tor proxy identity', JSON_PRETTY_PRINT);
return false;
} else {
fwrite($fp, 'AUTHENTICATE\n');
$received = fread($fp, 512);
fwrite($fp, 'signal NEWNY\n');
$received = fread($fp, 512);
}

?>
  • Create cURL Request with Tor Connection – Example code to Get current page URL
<?php

$url = 'http://example.com'

$torSocks5Proxy = 'socks5://127.0.0.1:9050';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_PROXY, $torSocks5Proxy);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
$data = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);

echo $data;

?>
  • That’s all Successfully Connect the TOR Proxy on PHP cURL Request

Here is the Example API Code to Get the blocked URL via Tor Connection using PHP cURL Request you will get the Output in JSON Format.

<?php

header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');
header('Content-type:application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: X-Requested-With');
header('X-Robots-Tag: noindex, nofollow', true);

$url = false;
$ip = '127.0.0.1';
$port = '9050';
$torSocks5Proxy = 'socks5://127.0.0.1:9050';
$msg = [];

if(isset($_GET['url'])){
$url = $_GET['url'];
}

$fp = fsockopen($ip, $port, $error_number, $err_string, 10);
if (!$fp) {
echo json_encode('Error while changing Tor proxy identity', JSON_PRETTY_PRINT);
return false;
} else {
fwrite($fp, 'AUTHENTICATE\n');
$received = fread($fp, 512);
fwrite($fp, 'signal NEWNY\n');
$received = fread($fp, 512);
}
fclose($fp);

try {
if (empty($url)) {
throw new Exception('false');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_PROXY, $torSocks5Proxy);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
$data = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
$msg['success'] = true;
if ($result == true) {
$msg['link']['web_url'] = $data;
} elseif ($result == false) {
$msg['link']['web_url'] = 'invalid url';
} else {
$msg = false;
}
echo json_encode($msg, JSON_PRETTY_PRINT);
}
catch(Exception $e) {
echo $e->getMessage();
}

I hope this tutorial will help you to connect Tor Proxy in PHP cURL Request.

 

by Team sanweb
Blogger - Web developer - Open Source Lover

Leave a Comment