I am using cloud server Ubuntu 12.04 as tor proxy server for scrapping purpose. The issue I am facing right now it is showing error
HTTP/1.0 501 Tor is not an HTTP Proxy Content-Type: text/html; charset=iso-8859-1
$url = 'http://whatismyipaddress.com';
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init('http://whatismyipaddress.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, 'https://127.0.01:9050/');
curl_exec($ch);
curl_close($ch);
$result=curl_exec($ch);
Need to help what I'm missing. I have already trying to use
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
a request is loading and session expire with no result.
This is true, Tor is not an HTTP proxy, but is instead a SOCKS v5 proxy.
Based on your cURL option CURLOPT_HTTPPROXYTUNNEL
, you are telling cURL to try to use Tor's proxy incorrectly (as an HTTP proxy).
The correct way would be to get rid of the proxy tunnel option and just set the proxy and SOCKS proxy type:
$proxy = '127.0.0.1:9050'; // no https:// or http://; just host:port
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
If you don't have PHP 5.5.23 or greater (which introduce CURLPROXY_SOCKS5_HOSTNAME
), you can use curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
If the cURL version PHP is compiled with is less than 7.18.0, it did not support SOCSK5 with hostname lookups, so you'll have to fall back to CURLPROXY_SOCKS5
and know that your DNS lookups will not go over Tor and potentially be exposed.
Side Note: I wrote a PHP library called TorUtils which provides a number of classes for interacting with Tor in PHP. One class is provides is the TorCurlWrapper
which abstracts the logic above and forces cURL to use Tor's SOCKS proxy correctly. There is a usage example here. You can install the library using composer require dapphp/torutils
or by downloading the package and adding it to your code.