33

I am having DNS issues with a certain target domain. I am using fopen() (but same issue with other functions) to retreive an image, but I get this error: Warning: fopen(): php_network_getaddresses: getaddrinfo failed: No such host is known

I am able to ping or nslookup the domain from the command prompt, but for some reason php throws this error. When I try fopen('http://www.google.com', r); or other domains, all goes fine. But above mentioned domain simply won't resolve with PHP. I have flushed the DNS, restarted Apache, but still no luck..

I have tried with:

  • Windows 7, Apache 2.2 PHP 5.3.6
  • Windows server 2008r2, Apache 2.2 PHP 5.3.6

What can cause this single domain to not resolve?

Richard
  • 4,341
  • 5
  • 35
  • 55
  • 2
    Maybe this is helpful: http://en.wikipedia.org/wiki/Getaddrinfo - and you should ask your system-administrator about the issue. – hakre Nov 21 '11 at 10:11
  • Perhaps the domain really doesn't exist, or the target domain's DNS server is unavailable to your server. You could try `ipconfig /flushdns` to clear out your cache, though. – Polynomial Nov 21 '11 at 10:12
  • Can you give a more specific example of the URL? (you can strip the domain name) – Narf Nov 21 '11 at 11:30
  • If you were under Linux, I'd say: dump the name of the file, then try to "wget" that file. But Windows.... – Olivier Pons Nov 21 '11 at 13:06
  • @Narf, I can't resolve the domain at all. Even at it's root. From the CMD there are no problems resolving the domain. From PHP I can resolve other domains though.. Peculiar.. DNS was flushed already. – Richard Nov 21 '11 at 13:11
  • 1
    @Richard, OK ... if even `fopen('http://domain.tld', 'r');` doesn't work - then I guess the URL format shouldn't be the problem. How is the server's internet connection configured? Is it via DHCP, static IP or some other method? – Narf Nov 21 '11 at 14:19
  • Hey man, i had the same error, it seems that this was because of an invalid address like: http://localhostmysite instead of http://localhost/mysite. Maybe your issue is caused by a similar problem. – Joshua Kissoon Jan 27 '12 at 03:16

8 Answers8

23

IMO it's the different way to resolve a name from the OS and PHP.

Try:

echo gethostbyname("host.name.tld");

and

var_export (dns_get_record ( "host.name.tld") );

or

$dns=array("8.8.8.8","8.8.4.4");
var_export (dns_get_record ( "host.name.tld" ,  DNS_ALL , $dns ));

You should found some DNS/resolver error.

Ivan Buttinoni
  • 4,110
  • 1
  • 24
  • 44
4

Your "localhost" cannot resolve the name www.google.com, which means your machine doesn't/can't reach a valid dns server.

Try ping google.com on the console of that machine to verify this.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
anil
  • 59
  • 1
2

It is more flexible to use curl instead of fopen and file_get_content for opening a webpage.

Mostafa Lavaei
  • 1,960
  • 1
  • 18
  • 27
1

getaddrinfo failed: No such host is known

The above error is caused due to mistake in Database Host (DB_HOST) in .env file

Open the .env file and Specially check for this env variables

DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password
Omkar Ghurye
  • 195
  • 1
  • 8
0

Your target domain might refuse to send you information. This can work as a filter based on browser agent or any other header information. This is a defense against bots, crawlers or any unwanted applications.

Szektor
  • 37
  • 1
  • 4
0

I had a lot of issues with this on Windows. I just performed git clone, copied my .env.example file to .env. updated it, and executed my trusty PHP script to update initial database used for a project. And it failed, and whatever I did, it kept on failing. Out of desperation, I reinstalled MySQL, but that didn't fix things.

Finally, I figured it out. PHP script extracted the settings from .env file using file_get_contents(), and exploding it using \n, but the .env file was encoded with CRLF, so my DB_HOST was empty. After I changed the encoding of the .env file, everything was fine.

So, my point is, echo the parameters supplied to mysqli::construct, and it might be the culprit.

domaci_a_nas
  • 220
  • 2
  • 11
-1

A weird thing I found was that the environment variable SYSTEMROOT must be set otherwise getaddrinfo() will fail on Windows 10.

bonger
  • 119
  • 1
  • 5
-4

What had caused this error on my side was the following line

include_once dirname(__FILE__) . './Config.php';

I managed to realize it was the culprit when i added the lines:

//error_reporting(E_ALL | E_DEPRECATED | E_STRICT);
//ini_set('display_errors', 1);

to all my php files.

To solve the path issue i canged the offending line to:

include_once dirname(__FILE__) . '/Config.php';
donohoe
  • 13,867
  • 4
  • 37
  • 59
nyxee
  • 2,773
  • 26
  • 22
  • Your original line and "changed" line are the same right now. – Noumenon Nov 01 '15 at 17:41
  • 1
    Your answer doesn't seem related at all.. Did you also get the message `Warning: fopen(): php_network_getaddresses: getaddrinfo failed: No such host is known` when using `include`? Pretty sure that's not possible – Richard Nov 01 '15 at 20:46
  • I realized that that mistake lead to a myriad of unrelated problems, That;s why i posted it as an answer to many different problems. – nyxee Nov 05 '15 at 05:01