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?

7
  • 2
    Maybe this is helpful: en.wikipedia.org/wiki/Getaddrinfo - and you should ask your system-administrator about the issue.
    – hakre
    Commented Nov 21, 2011 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
    Commented Nov 21, 2011 at 10:12
  • Can you give a more specific example of the URL? (you can strip the domain name)
    – Narf
    Commented Nov 21, 2011 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.... Commented Nov 21, 2011 at 13:06
  • 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
    Commented Nov 21, 2011 at 14:19

8 Answers 8

24

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.

0
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.

2

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

1

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.

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
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.

-1

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

-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';
3
  • Your original line and "changed" line are the same right now.
    – Noumenon
    Commented Nov 1, 2015 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
    Commented Nov 1, 2015 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
    Commented Nov 5, 2015 at 5:01

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