我正在尝试从另一个主机获取页面。我照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 (最近的公共域名)。
发布于 2015-10-13 00:41:28
看起来你的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;发布于 2022-02-13 09:11:09
即使在allow_url_fopen打开的时候,我也遇到了同样的file_get_contents问题。另外,CURL在主机上也有类似的问题。
这是网站所有者如何解决这个问题的:
“需要向Directadmin添加额外的A记录,显示了www,但不是没有www:
example.com。A xxx.xxx“
https://stackoverflow.com/questions/33085948
复制相似问题