为什么 cURL 将无效的 IP 地址报告为错误?

发布于 2024-09-24 02:43:35 字数 442 浏览 1 评论 0原文

我有以下使用 cURL 的 PHP 代码:

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,"http://area51.stackexchange.com/users/flair/31.json");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

$a_data = json_decode(curl_exec($ch));

echo curl_error($ch);

当我尝试通过 HTTP 访问该页面时,出现以下错误:

无法连接到 0.0.0.31:参数无效

但是,从命令行运行时代码可以正常工作。

什么可能导致 cURL 尝试连接到 0.0.0.31(据我所知),甚至不是一个有效的 IP 地址?

I have the following PHP code that uses cURL:

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,"http://area51.stackexchange.com/users/flair/31.json");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

$a_data = json_decode(curl_exec($ch));

echo curl_error($ch);

I then get the following error when I try to access the page over HTTP:

Failed to connect to 0.0.0.31: Invalid argument

However, the code works fine when run from the command line.

What could possible cause cURL to try to connect to 0.0.0.31, which is AFAIK, not even a valid IP address?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耳钉梦 2024-10-01 02:43:35

“什么可能导致 cURL 尝试连接到 0.0.0.31,据我所知,甚至不是一个有效的 IP 地址?”

你的 DNS 出了问题。我测试了你的代码并且它有效。

"What could possible cause cURL to try to connect to 0.0.0.31, which is AFAIK, not even a valid IP address?"

Your DNS is botched. I tested your code and it works.

坚持沉默 2024-10-01 02:43:35

当您说“从命令行运行时代码工作正常”时,您的意思是您已经使用 PHP CLI 解释器运行了它吗?如果是这种情况,您可以检查 Web 服务器上 php -i 的输出与 phpinfo() 的输出之间是否存在任何明显的不匹配。这可能是一些奇怪的版本不匹配或环境问题,尽管你的猜测和我的一样好。

如果您(正如我最初的想法)只是谈论运行 curl 命令,您也可以尝试检查版本号或环境变量。

When you say "the code works fine when run from the command line," do you mean you have run it with the PHP CLI interpreter? If that's the case, you might check for any noticeable mismatches between the output of php -i vs. phpinfo() on the web server. It might be some strange version mismatch or environment problem, although your guess is as good as mine.

If you are (as I had originally thought) just talking about running the curl command, you could try checking version numbers or environment variables there too.

好多鱼好多余 2024-10-01 02:43:35

我也有同样的问题。

我认为您可能所做的就是像这样构建 url:

$url = "http://area51.stackexchange.com/users/flair/" + 31 + ".json";

由于某种原因,然后 url 被转换为 0.0.0.31。

相反,尝试连接字符串,如下所示:

$url = "http://area51.stackexchange.com/users/flair/" . 31 . ".json";

至少解决了我的问题!

I had the same problem.

I think what you might have done was to construct the url like so:

$url = "http://area51.stackexchange.com/users/flair/" + 31 + ".json";

And for some reason the url then is translated to 0.0.0.31.

Instead try to concatinate the string, like so:

$url = "http://area51.stackexchange.com/users/flair/" . 31 . ".json";

Solved my issue at least!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文