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

时间:2018-08-09
本文章向大家介绍如何使用PHP中的cURL函数连接到Tor隐藏服务,需要的朋友可以参考一下。

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

$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);

当我运行它时,我收到以下错误:

Couldn't resolve host name

但是,当我从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(代理)正在解析它识别的.onion主机名。在运行上面的PHP代码时,我的猜测是cURL或PHP正在尝试解析.onion主机名并且无法识别它。我已经搜索了一种告诉cURL / PHP让代理解析主机名的方法,但我找不到办法。

看起来CURLPROXY_SOCKS5_HOSTNAME没有在PHP中定义,但您可以显式使用其值,它等于7:

curl_setopt($ch, CURLOPT_PROXYTYPE, 7);