file_get_contents 失败 "getaddrinfo failed: no address associated with hostname"

file_get_contents fails with "getaddrinfo failed: no address associated with hostname"

我正在尝试从其他主机获取页面。我按照 hph 手册说的那样做:

    $page = file_get_contents('http://www.example.com/');
    echo $page;

但它失败了,在 apache 日志中我得到以下信息:

[Mon Oct 12 18:58:47.676454 2015] [:error] [pid 2971] [client 127.0.0.1:49434] PHP Warning:  file_get_contents(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /var/www/html/digest/ftry.php on line 2
[Mon Oct 12 18:58:47.704659 2015] [:error] [pid 2971] [client 127.0.0.1:49434] PHP Warning:  file_get_contents(http://www.example.com/): failed to open stream: php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /var/www/html/digest/ftry.php on line 2

在试图找出失败的原因时,我检查了 nslookup 所说的内容。当我 运行 nslookup http://www.example.com/ 它回答类似这样的问题:

nslookup http://www.example.com/
Server:         94.242.57.130
Address:        94.242.57.130#53

** server can't find http://www.example.com/: NXDOMAIN

当我从域名中删除 http:// 时它工作正常,returns ip。但是当我尝试在没有 http:// 的情况下使用 file_get_contents 获取文件时,它会在本地文件系统中查找文件(它应该是这样的)。

我已经检查过 phpinfoallow_url_fopen 在本地和大师级别上都是 On。我已将 resolv.conf 中的名称服务器设置为 94.242.57.130(最近的 public dns)。

php.ini 似乎禁用了 allow_url_fopen,这就是 file_get_contents 无法正常工作的原因。
尝试使用 curl 代替:

$url = "http://www.example.com/";
$ch = curl_init();  
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$page = curl_exec($ch);
curl_close($ch);
echo $page;

即使 allow_url_fopen 处于打开状态,我也遇到了 file_get_contents 的相同问题。也因为 CURL 与主机有类似的问题。

以下是网站所有者解决此问题的方法:

" 需要给Directadmin增加一条额外的A记录,出现了www但不是没有www:

example.com。一个xxx.xxx.xxx.xxx“