How can I connect to a Tor hidden service using cURL in PHP?
You need to set option CURLOPT_PROXYTYPE
to CURLPROXY_SOCKS5_HOSTNAME
, which sadly wasn't defined in old PHP versions, circa pre-5.6; if you have earlier in but you can explicitly use its value, which is equal to 7
:
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
I use Privoxy and cURL to scrape Tor pages:
<?php $ch = curl_init('http://jhiwjjlqpyawmpjx.onion'); // Tormail URL curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($ch, CURLOPT_PROXY, "localhost:8118"); // Default privoxy port curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); curl_exec($ch); curl_close($ch);?>
After installing Privoxy you need to add this line to the configuration file (/etc/privoxy/config
). Note the space and '.' a the end of line.
forward-socks4a / localhost:9050 .
Then restart Privoxy.
/etc/init.d/privoxy restart