php - 如何在 PHP 中使用 cURL 连接到 Tor 隐藏服务?

标签 php curl proxy 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);

当我运行它时,我收到以下错误:

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/

相关文章:

php - 搜索引擎不适用于我的分页器

php - 从双重缩短的 URL 获取最终 URL (t.co -> bit.ly -> final)

ssl - 网站适用于 chrome 但不适用于 curl

php - 有没有办法让 cURL 等到页面的动态更新完成?

Golang tcp-tls over socks5 代理客户端

java - 具有 0 个或多个任意类型参数的 Spring AOP 切入点表达式方法会抛出 java.lang.NullPointerException

php mysql 行依赖性检查

php - 如何最好地修改带有表前缀的sql查询

Python3 请求不使用传入代理

javascript - 删除 cookie(PHP、JS)适用于 Firefox、Safari、Opera,但不适用于 Chrome