我正在尝试从其他主机获取页面。我按照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://
时,它工作正常,返回ip。但是,当我试图获得没有http://
的file_get_contents
文件时,它会在本地文件系统中查找该文件(这就是它应该是怎样的)。
我已经检查了phpinfo
, allow_url_fopen
是On
在本地和主级别。我已将resolv.conf
中的名称服务器设置为94.242.57.130
(最近的公共dns)。
似乎allow_url_fopen
在你的php.ini
上是禁用的,这就是为什么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
的相同问题。
网站所有者是这样解决这个问题的:
"需要在Directadmin中添加一个额外的A记录,www出现了,但没有www:
example.com。A xxx.xxx.xxx.xxx "