我正在尝试使用以下 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 让代理解析主机名,但我找不到方法。
有一个非常相似的 Stack Overflow 问题, cURL request using socks5 proxy fails when using PHP, but it works through the command line .
最佳答案
您需要将选项 CURLOPT_PROXYTYPE
设置为 CURLPROXY_SOCKS5_HOSTNAME
,可悲的是wasn't defined in old PHP versions, circa pre-5.6 ;如果你之前有,但你可以显式使用它的值,它等于 7
:
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
关于php - 如何在 PHP 中使用 cURL 连接到 Tor 隐藏服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15445285/