views:

450

answers:

2

I'm working with an OS X 10.6 Server running Apache2 / PHP5 and having a problem with PHP not resolving hostnames when 'fopen()' tries to retrieve a file from a remote server. When run in interactive mode on the command line 'fopen()' works perfectly. However, when run through the web it will always fail with the error:

failed to open stream: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known

I'm at a loss in finding the source of this problem: 'fopen()' works on the web when given an IP address instead of a hostname; 'gethostbyname()' also fails when run on the web (it doesn't error, it just returns whatever hostname it was given to resolve) but also works fine when run in interactive mode. The only exception seems to be 'dns_get_record()' which works fine when run on the web or in interactive mode.

I've been trying to find DNS problems on the server but dig, nslookup, and ping all work and "scutil -r" says the remote server is reachable with the current DNS settings. Any ideas on where the problem might be?

+1  A: 

You must set properly the allow_url_fopen value of your php.ini.

http://ar.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

Consider the CLI uses a different php.ini that the webserver (I assume you are using Apache).

Another option may be you are not including libnss_dns

Try adding to your httpd configuration (correct path as needed for you system):

LoadFile /lib/libnss_dns.so
Johnco
I don't think allow_url_fopen() is the problem here, but comparing the PHP.ini's is a good idea.
Pekka
Apologies, I should have explicitly stated that I'd checked the PHP.ini in use by Apache to be certain allow_url_fopen() was on. I'm looking into 'libnss_dns.so' but I've been unable to locate it in OS X 10.6 or the MacPorts distribution of Apache2.I didn't think it important when first posting, but this problem first occurred a few months ago and I was able to work around it by adding the remote server to '/etc/hosts'. Whatever the problem is now, manually editing the 'hosts' file doesn't seem to help.
Kaja
I was also getting this error with MacPorts 1.9.1. Installing ldns (sudo port install ldns) fixed it for me.
richardkmiller
A: 

Try to recompile PHP with '--disable-ipv6' option. Then, apachectl stop & start (not 'apachectl restart'). I've got the same error message on Mac OS 10.6.4 + manually compiled PHP5.3.3 + Apache2.2.16.

Nobu