Ivar
Ivar

Reputation: 4392

PHP: file_get_contents() gives me "Name or service not known"

Okay, this question has been asked plenty of times around the internet, but I haven't found any solution that fixes the problem in my case.

I'm about to fetch some JSON data with this code:

$response = file_get_contents('http://www.domain.com/path/to/json');

And it's constantly stumbling over the following error:

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

I'm running Debian with PHP 5.4.4 on Nginx 1.2.6 and php-fpm.

This is my troubleshooting so far:

  1. The URL exists and gives a valid result in the browser.
  2. I've ensured that allow_url_fopen is on in php.ini.
  3. I've changed to Google Public DNS in /etc/resolv.conf.
  4. Doing ping google.com works.
  5. Switching to http://0.0.0.0/path/to/json works but is undesirable.
  6. I've got a couple of domains pointing to the same server, and the same problem occurs independently.

I'm a bit concerned about the domain in /etc/resolv.conf, is it right?

# Automatically generated by OnApp (2.3.0-29)
domain localdomain

# Original DNS
#nameserver 10.0.10.10
#nameserver 10.0.11.11

# Google Public DNS
nameserver 8.8.8.8
nameserver 8.8.4.4

Any kind of help is appreciated.

Upvotes: 1

Views: 6137

Answers (3)

Muktadir
Muktadir

Reputation: 115

I got it solved in two ways, either run php (or apache) in root user mode or update your debian package. It's probably debian7u5 or update 4 who has this bug.

Upvotes: 0

Ivar
Ivar

Reputation: 4392

I hate to say this, but the problem solved itself; I just upgraded the Debian packages (apt-get upgrade) and the problem went away. Frustrating to not know the cause, though.

Thank you all for your time!

Upvotes: 1

BogdanM
BogdanM

Reputation: 635

It's most likely a DNS problem. If IP works but domain has problems you should look into the DNS. That's what "name or service not known" hints to. If you can, use different nameservers. I think google's dns can't translate your domain to the ip.

Upvotes: 0

Related Questions