如何在 PHP 中使用 cURL 连接到 Tor 隐藏服务? [英] How can I connect to a Tor hidden service using cURL in PHP?

查看:29
本文介绍了如何在 PHP 中使用 cURL 连接到 Tor 隐藏服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to connect to a Tor hidden service using the following PHP code:

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

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

When I run it, I get the following error:

无法解析主机名

但是,当我在 Ubuntu 中从命令行运行以下命令时:

However, when I run the following command from my command line in Ubuntu:

curl -v --socks5-hostname localhost:9050 http://jhiwjjlqpyawmpjx.onion

我得到了预期的回复.

PHP cURL 文档是这样说的:

The PHP cURL documentation says this:

--socks5-hostname
Use  the  specified  SOCKS5 proxy (and let the proxy resolve the host name).

我相信它从命令行工作的原因是因为 Tor(代理)正在解析它识别的 .onion 主机名.运行上面的 PHP 代码时,我猜测是 cURL 或 PHP 试图解析 .onion 主机名但无法识别它.我已经寻找了一种方法来告诉 cURL/PHP 让代理解析主机名,但我找不到方法.

I believe the reason it works from the command line is because Tor (the proxy) is resolving the .onion hostname, which it recognizes. When running the PHP code above, my guess is that cURL or PHP is trying to resolve the .onion hostname and doesn't recognize it. I've searched for a way to tell cURL/PHP to let the proxy resolve the hostname, but I can't find a way.

有一个非常相似的堆栈溢出问题,使用 PHP 时使用socks5 代理的cURL 请求失败,但它可以通过命令行运行.

There is a very similar Stack Overflow question, cURL request using socks5 proxy fails when using PHP, but it works through the command line.

推荐答案

您需要将选项 CURLOPT_PROXYTYPE 设置为 CURLPROXY_SOCKS5_HOSTNAME,遗憾的是 在旧的 PHP 版本中没有定义,大约在 5.6 之前;如果你之前有但你可以明确地使用它的值,它等于 7:

You need to set option CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME, which sadly wasn't defined in old PHP versions, circa pre-5.6; if you have earlier in but you can explicitly use its value, which is equal to 7:

curl_setopt($ch, CURLOPT_PROXYTYPE, 7);

这篇关于如何在 PHP 中使用 cURL 连接到 Tor 隐藏服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆