4

I am having problems with the following code:

function http_file_exists($url){
$f=fopen($url,"r");
if($f){
    fclose($f);
    return true;
} else {
  return false;
}

} $url = "http://www.minhemmelighed.dk/Graphics/Products/55.jpg";

print http_file_exists($url);

The error it returns is:

Warning: fopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /srv/http/webshop3/image_scraper/test.php on line 6 Warning: fopen(http://www.minhemmelighed.dk/Graphics/Products/55.jpg): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /srv/http/webshop3/image_scraper/test.php on line 6

What can I do about this? I have rebooted the server several times.

For the record:

I can access the website in question through a browser

I am running LAMP on Arch Linux

Olive
  • 3,516
  • 7
  • 24
  • 32
  • 1
    Does DNS work on the server too? How did you test that you coan access the website in question? – phihag Aug 01 '11 at 11:24
  • DNS should work, I think? How can I test that? :) I accessed the website in my browser without problems – Olive Aug 01 '11 at 11:31
  • See my answer for a way to test DNS. Don't forget to test it *on the server as the php user*. (Oh, and try not to claim it works if you didn't actually test it). – phihag Aug 01 '11 at 11:40

2 Answers2

7

Rebooting a UNIX server is extremely unlikely to fix a problem. It looks like you need to check your nameserver configuration on the server. On the console of the web server (logged in as the php user, probably apache or www-data), test

dig www.minhemmelighed.dk

If this outputs an error message, check your /etc/resolv.conf. Comment out all lines and add

nameserver 8.8.8.8 # Google's public DNS server

If that solves the problem, contact the administrator of the original nameserver (or just use Google's 8.8.8.8). If it doesn't, check your connectivity and firewalls.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • Thanks :) But it didn't fix the problem :S I am afraid that it's an Apache Server error :( – Olive Aug 01 '11 at 11:12
  • 1
    @Eax At what step did you fail? I.e. did the `dig` work? The problem is almost certainly completely unrelated to the HTTP server, Apache or otherwise. – phihag Aug 01 '11 at 11:21
  • Sorry :) Dig didn't fail, I can also visit minhemmelighed.dk in my browser without a problem :) – Olive Aug 01 '11 at 11:27
  • @Eax If `dig` succeeds on the *server* as the *php user* (`su apache` or `su www-data`), can you post a link to the output of `strace php -r 'file_get_contents("http://www.minhemmelighed.dk/Graphics/Products/55.jpg");'` at http://pastebin.com or so? – phihag Aug 01 '11 at 11:39
  • Neither of those users exist :S – Olive Aug 01 '11 at 11:41
  • @Eax Use `ps -A u | grep apache` to find out the user name (in the first column). – phihag Aug 01 '11 at 11:42
  • @Eax The paste is executed as the user `eax`. Is that the user php's running as? And it shows DNS and actual HTTP download working, so it's obviously not the problematic configuration. – phihag Aug 01 '11 at 11:52
  • That is what ps -A u | grep apache returns yes, (so yes, it is executed as eax) – Olive Aug 01 '11 at 11:55
  • Many thanks. This has been bugging me for a couple of hours and the google DNS fixed it. – t j Jun 19 '12 at 11:02
3

For those of you running a very locked down distro like CentOS it is probably a security feature of SELinux as I discovered on my systems.

At the command prompt type:

getsebool -a | grep httpd

and look for

httpd_verify_dns --> off

if this is the case you will need to set it On with the following command

setsebool httpd_verify_dns=1

or permanently with:

setsebool -P httpd_verify_dns=1

remember to restart httpd with:

service httpd restart after every change

I hope that helps

Mark

Mark
  • 166
  • 1
  • 13