【发布时间】:2013-03-04 22:16:01
【问题描述】:
我正在尝试使用以下 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);
当我运行它时,我得到以下错误:
无法解析主机名
但是,当我在 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。
【问题讨论】: