Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a script in PHP that's been running fine for months. It recently stopped working.

This script connects to gmail to send out an email to my customers.

Recently, I started getting this error when I run the script:

The SMTP connection failed to start [tls://]: 
fsockopen returned Error Number 0 and Error String 'php_network_getaddresses: 
getaddrinfo failed: Temporary failure in name resolution'

Keep in mind, this was with zero code changes.

I've looked at my resolv.conf and it appears to be ok:


I can ping

# ping
PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=247 time=26.7 ms

I can connect via lynx to google and other sites with no problem.

I've logged into my gmail account with no problems (no captcha there either).

I am at wits end. Anyone have any ideas?


share|improve this question
Is the script failing all the time? Or just occasionally? – MikeyB May 31 '09 at 3:29
Fails every time. – GeoffreyF67 May 31 '09 at 3:37
I encountered similar issue with my daemon written in C which performs periodic TCP reconnections. At some point getaddrinfo() suddenly started to return the error. When I looked at the server, no DNS requests were being sent, and adding the required entry into /etc/hosts did not help. Sure, restart helps but as Xerxes rightfully noted, this is not the real solution. Initially I thought that the problem was caused by missing freeaddrinfo() call but I failed to reproduce it with a test application. Anyway, I've added proper cleanup calls to the daemon and going to monitor it closely. – Linulin Dec 21 '09 at 11:54
up vote 8 down vote accepted

PHP is having trouble accessing either /etc/hosts or /etc/resolv.conf: there's a long standing issue in PHP related to this specific error. The fix is to try restarting Apache or whatever is invoking PHP, or to make sure /etc/hosts and /etc/resolv.conf are readable by what's invoking PHP.

share|improve this answer
Restart did the trick! – GeoffreyF67 May 31 '09 at 4:10
Yes, but how long before the next restart is due? I would look further and find the root cause of the problem. – Xerxes May 31 '09 at 5:06
Wow I was having the same issue, but apparently service httpd restart is the solution... but I still doesn't understand what causes this... – J_Wesker Aug 9 '10 at 1:32
% dig @ +short

Now, try using Xdebug to see where the problem is exactly....

$ip = gethostbyname('');

Anything good in the logs?

share|improve this answer

I just experienced the same error and

service httpd restart

did the trick...

share|improve this answer

Add debugging code before that line to make sure that the script can resolve it correctly.

share|improve this answer

I have had this fault today, however it occurred after a specific event which makes me think that i may have found the cause.

Due to some network equipment trouble, i actually rebooted by server and when it came back up, there was an incorrect network connection in the cabling, effectively meaning no DNS server was available.

in the meantime this php function was called and after this it never worked again until i restarted httpd.

I think the bug may relate to a network failure (where the DNS is not only not available, but unreachable, ie wrong subnet) and when the network subsequently recovers this function does not look for the DNS again.

This thead was a few years ago, maybe this bug is now fixed in PHP?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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