How can I connect to a Tor hidden service using cURL in PHP?

I'm trying to connect to a Tor hidden service using the following PHP code:

$url = 'http://jhiwjjlqpyawmpjx.onion/'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:9050/");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);

print_r($output);
print_r($curl_error);

When I run it, I get the following error:

Couldn't resolve host name

However, when I run the following command from my command line in Ubuntu:

curl -v --socks5-hostname localhost:9050 http://jhiwjjlqpyawmpjx.onion

I get a response as expected

The PHP cURL documentations says this:

--socks5-hostname
Use  the  specified  SOCKS5 proxy (and let the proxy resolve the host name).

I believe the reason it works from the command line is because Tor (the proxy) is resolving the .onion hostname, which it recognizes. When running the PHP code above, my guess is that cURL or PHP is trying to resolve the .onion hostname and doesn't recognize it. I've searched for a way to tell cURL/PHP to let the proxy resolve the hostname, but I can't find a way.

There is a very similar Stack Overflow question, cURL request using socks5 proxy fails when using PHP, but it works through the command line.


看起来CURLPROXY_SOCKS5_HOSTNAME没有在PHP中定义,但是您可以明确地使用它的值,该值等于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

尝试添加这个:

curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 
链接地址: http://www.djcxy.com/p/8536.html

上一篇: 问题从cURL命令行将JSON文档从文件加载到CouchDB中

下一篇: 如何在PHP中使用cURL连接到Tor隐藏服务?