为什么 cURL 将无效的 IP 地址报告为错误?
我有以下使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“什么可能导致 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.
当您说“从命令行运行时代码工作正常”时,您的意思是您已经使用 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.我也有同样的问题。
我认为您可能所做的就是像这样构建 url:
由于某种原因,然后 url 被转换为 0.0.0.31。
相反,尝试连接字符串,如下所示:
至少解决了我的问题!
I had the same problem.
I think what you might have done was to construct the url like so:
And for some reason the url then is translated to 0.0.0.31.
Instead try to concatinate the string, like so:
Solved my issue at least!