13

I'm trying to update some values from one server to another server with a cron job. I use 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

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

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.

Does anyone know how to solve this?

2

3 Answers 3

12

In my case, the trouble was that Apache get the DNS entries from /etc/resolv.conf when 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.

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

2
  • Thank you Sir that was finally my solution.
    – Sam Tuke
    Jun 7, 2016 at 11:14
  • I'll third it: That was also my problem. Likely because I'm doing my development on a VM so things change when I resume the VM. Feb 4, 2018 at 2:05
1

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.

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

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

0

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.

1
  • 1
    This is incorrect. Error clearly states, that name resolving failed. Sep 20, 2019 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.