php file_get_contents(): php_network_getaddresses: getaddrinfo 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23386364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:41:26  来源:igfitidea点击:

file_get_contents(): php_network_getaddresses: getaddrinfo failed

phpdnsfile-get-contents

提问by Anorionil

I'm trying to update some values from one server to another server with a cron job. I use file_get_contents().

我正在尝试使用 cron 作业将一些值从一台服务器更新到另一台服务器。我使用 file_get_contents()。

The strange thing is that this fails occasionally. One minute it works, another minute it won't work. I receive these two errors:

奇怪的是,这偶尔会失败。一分钟有效,一分钟无效。我收到这两个错误:

PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known

PHP Warning: file_get_contents(): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known

PHP 警告:file_get_contents():php_network_getaddresses:getaddrinfo 失败:名称或服务未知

PHP 警告:file_get_contents():无法打开流:php_network_getaddresses:getaddrinfo 失败:名称或服务未知

and:

和:

PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname

PHP Warning: file_get_contents(): failed to open stream: php_network_getaddresses: getaddrinfo failed: No address associated with hostname

PHP 警告:file_get_contents():php_network_getaddresses:getaddrinfo 失败:没有与主机名关联的地址

PHP 警告:file_get_contents():无法打开流:php_network_getaddresses:getaddrinfo 失败:没有与主机名关联的地址

I've logged into the server that experiences this problem. Using ping or traceroute works fine, so I've ruled out DNS issues. I've also added the google DNS nameservers just to be sure. A third server that uses the exact same code doesn't have this problem, so it looks to be a problem on the "calling" end, not the "responding" end. Replacing the hostname with IP is not an option for this case.

我已登录遇到此问题的服务器。使用 ping 或 traceroute 工作正常,所以我排除了 DNS 问题。为了确定起见,我还添加了 google DNS 名称服务器。使用完全相同代码的第三台服务器没有这个问题,所以它看起来是“调用”端的问题,而不是“响应”端的问题。在这种情况下,不能用 IP 替换主机名。

Does anyone know how to solve this?

有谁知道如何解决这个问题?

回答by Diogo Melo

In my case, the trouble was that Apache get the DNS entries from /etc/resolv.confwhen it starts. By the time I started my Apache, the network was not set yet. Restarting Apache (after networking is configured) solved the problem for me.

就我而言,问题在于 Apache 从/etc/resolv.conf启动时获取 DNS 条目。当我启动我的 Apache 时,网络还没有设置。重新启动Apache(配置网络后)为我解决了这个问题。

I explained the case in more details on my blog post http://diogomelo.net/blog/16/Warning-file_get_contents-php_network_getaddresses-getaddrinfo-failed-No-address-associated-with-hostname

我在我的博客文章http://diogomelo.net/blog/16/Warning-file_get_contents-php_network_getaddresses-getaddrinfo-failed-No-address-related-with-hostname上更详细地解释了这个案例

回答by TheJosh

The name lookup appears to fail whilst running within a cron job sometimes. I've only seen it with names in /etc/hosts, but I haven't tested DNS names.

有时在 cron 作业中运行时,名称查找似乎会失败。我只在 /etc/hosts 中看到过名称,但我没有测试过 DNS 名称。

I was seeing it with sending email and SMTP connection timeouts.

我看到它发送电子邮件和 SMTP 连接超时。

I don't know the exact cause, but in my case I got my SMTP code to connect to 127.0.0.1instead of localhostand the problem went away.

我不知道确切的原因,但在我的情况下,我让我的 SMTP 代码连接到127.0.0.1而不是,localhost问题就消失了。

回答by Pmpr

This is because of connection limits on 2nd server, and I believe that you have set short time intervals for the cron job. As you know, servers control consecutive connections from each IP. You may choose a longer time interval to resolve this problem.

这是因为第二台服务器的连接限制,我相信您为 cron 作业设置了较短的时间间隔。如您所知,服务器控制来自每个 IP 的连续连接。您可以选择更长的时间间隔来解决此问题。