如何在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);

当我运行它时,出现以下错误:

无法解析主机名称

但是,当我在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问题,当使用PHP时,使用socks5代理的cURL请求失败,但它通过命令行工作。


看起来CURLPROXY_SOCKS5_HOSTNAME没有在PHP中定义,但是您可以明确地使用它的值,该值等于7:

curl_setopt($ch, CURLOPT_PROXYTYPE, 7);

我用Privoxy和cURL刮Tor页面:

<?php
    $ch = curl_init('http://jhiwjjlqpyawmpjx.onion'); // Tormail URL
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_PROXY, "localhost:8118"); // Default privoxy port
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    curl_exec($ch);
    curl_close($ch);
?>

安装Privoxy后,您需要将此行添加到配置文件( /etc/privoxy/config )中。 注意空间和'。' 行结束。

forward-socks4a / localhost:9050 .

然后重新启动Privoxy。

/etc/init.d/privoxy restart

尝试添加这个:

curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 
链接地址: http://www.djcxy.com/p/8535.html

上一篇: How can I connect to a Tor hidden service using cURL in PHP?

下一篇: CURL CouchDB Replication command