我正在尝试使用以下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);
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到以下错误:
无法解析主机名
但是,当我从Ubuntu中的命令行运行以下命令时:
curl -v --socks5-hostname localhost:9050 http://jhiwjjlqpyawmpjx.onion
Run Code Online (Sandbox Code Playgroud)
我得到了预期的回复
PHP cURL文档说明了这一点:
--socks5-hostname
Use the specified SOCKS5 proxy (and let the proxy resolve the host name).
Run Code Online (Sandbox Code Playgroud)
我相信它从命令行工作的原因是因为Tor(代理)正在解析它识别的.onion主机名.在运行上面的PHP代码时,我的猜测是cURL或PHP正在尝试解析.onion主机名并且无法识别它.我已经搜索了一种告诉cURL/PHP让代理解析主机名的方法,但我找不到办法.
有一个非常类似的Stack Overflow问题,使用socks5代理的cURL请求在使用PHP时失败,但它通过命令行工作.