PHP php_network_getaddresses: getaddrinfo failed:

2019-01-07 14:41发布

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?

5条回答
2楼-- · 2019-01-07 15:15

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';
查看更多
Summer. ? 凉城
3楼-- · 2019-01-07 15:22

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.

查看更多
三岁会撩人
4楼-- · 2019-01-07 15:22

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

查看更多
贼婆χ
5楼-- · 2019-01-07 15:31

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.

查看更多
放我归山
6楼-- · 2019-01-07 15:32

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

查看更多
登录 后发表回答