DNS not working in PHP (but does in general)

旧城冷巷雨未停 提交于 2020-01-24 17:23:05

问题


The following script:

<?php
 $dnsrecs = dns_get_record('google.com'); foreach($dnsrecs as $dnsrec){ foreach($dnsrec as $label=>$val){ echo $label.':'. $val . '<br>'; } }


  echo file_get_contents('https://google.com'); 


?>

Gives back a list of DNS records, but ends up failing on the file_get_contents, although phpinfo() says:

allow_url_fopen On  On
allow_url_include   Off

With the following error:

PHP message: PHP Warning: file_get_contents(https://google.com): failed to open stream: php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /var/www/a.php on line 5" while reading response header from upstream, client: 152.66.34.10, server: xxx, request: "GET /a.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm-www.sock:", host: "xxx"

DNS resolution on the server works, with the same user as nginx is running:

# sudo -u www-data host google.com
google.com has address 216.58.212.142
google.com has IPv6 address 2a00:1450:400e:800::200e
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.

My PHP version is recent:

# php --version
PHP 5.5.9-1ubuntu4.21 (cli) (built: Feb  9 2017 20:54:58) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

I am running it via php5-fpm, and it seems to be related to that, because sudo -u www-data php a.php works correctly.

My php.ini file is here: http://pastebin.com/qZePJdT5 And my phpinfo is here: http://pastebin.com/cAWuu8wr

Thanks.


回答1:


We were seeing this exact issue with the Goolge public DNS servers. We fixed it by simply restarting PHP-FPM. This issue has been the topic of some twitter disucssions:

https://twitter.com/laravelphp/status/844181376224165890

However is does not seem to be DigitalOcean specific.




回答2:


Rebooting server (linux) and changing nothing else fixed this.



来源:https://stackoverflow.com/questions/42926060/dns-not-working-in-php-but-does-in-general

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!