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

问题描述:

我尝试使用以下PHP代码连接到Tor隐藏服务:

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

但是,当我运行以下命令我的命令行在Ubuntu:

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

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

我得到一个预期的回应

PHP cURL 文档说明:

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



我相信从命令行工作的原因是因为Tor(代理)正在解决。洋葱主机名,它识别。当运行上面的PHP代码,我的猜测是cURL或PHP正试图解析.onion主机名,并不认识它。我搜索了一个方法来告诉cURL / PHP让代理解析主机名,但我找不到一个办法。

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.

有一个非常相似的堆栈溢出问题, 使用socks5代理的cURL请求在使用PHP时失败,但通过命令行

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:

Looks like CURLPROXY_SOCKS5_HOSTNAME is not defined in PHP, but you can explicitly use its value, which is equal to 7:

curl_setopt($ch, CURLOPT_PROXYTYPE, 7);