PHP:file_get_contents($loc) 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/542046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP : file_get_contents($loc) fails
提问by Sampson
I just moved a project from localhost over to my remote server, and noticed that some of my scripts stopped working. Most importantly was one that relied upon file_get_contents()to fetch JSON values from another script.
我刚刚将一个项目从 localhost 移到我的远程服务器,并注意到我的一些脚本停止工作。最重要的是依赖于file_get_contents()从另一个脚本获取 JSON 值。
PHP Version is 5.2.4allow_url_fopenis ON
PHP 版本为 5.2.4allow_url_fopen已开启
Warning:
file_get_contents()[function.file-get-contents]:php_network_getaddresses:getaddrinfofailed: Name or service not known in/var/www/html/2009/functions/functions.products.phponline 5Warning:
file_get_contents(http://data.example.com/new-data.php) [function.file-get-contents]: failed to open stream:Success in /var/www/html/2009/functions/functions.products.phponline 5
警告:
file_get_contents()[function.file-get-contents]:php_network_getaddresses:getaddrinfo失败:产品名称或服务不知道/var/www/html/2009/functions/functions.products.php的line 5警告::
file_get_contents(http://data.example.com/new-data.php) [function.file-get-contents]无法打开流:Success in /var/www/html/2009/functions/functions.products.php打开line 5
The script is being ran from: http://www.example.com
The location passed into the function is http://data.example.com/new-data.php
正在运行脚本:http://www.example.com
传递给函数的位置是http://data.example.com/new-data.php
Note: Same domain name, but two different servers.
注意:相同的域名,但两个不同的服务器。
function getData() {
$location = "http://data.mysite.com/new-data.php";
$contents = file_get_contents($location);
$jsonVars = json_decode($contents);
return $jsonVars
}
回答by bobince
Name or service not known
名称或服务未知
DNS is broke. Can you ping data.mysite.comfrom a shell on the machine (assuming you have one)?
DNS 坏了。您可以ping data.mysite.com从机器上的外壳(假设您有一个)吗?
Try replacing data.mysite.comwith a fixed IP address for now.
暂时尝试用data.mysite.com固定 IP 地址替换。
回答by Rytis
Also you can try curl:
你也可以尝试卷曲:
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'http://url.url');
$result = curl_exec($curl);
curl_close($curl);
And you get what you want in $result.
你会得到你想要的$result。
回答by pfrenssen
If you are sure it is not a DNS issue, try restarting Apache. This solved it for me after 20 minutes of head scratching.
如果您确定这不是 DNS 问题,请尝试重新启动 Apache。经过20分钟的挠头后,这为我解决了这个问题。
回答by chaos
Look at your /etc/hosts on the remote server. If it's empty, you need to add '127.0.0.1 localhost' to it.
查看远程服务器上的 /etc/hosts。如果为空,则需要在其中添加“127.0.0.1 localhost”。
Unless it's one of the varieties of VPS where the loopback interface hits the outer machine; on those, you need to use your VPS's IP number instead of 127.0.0.1.
除非是回环接口打到外机的VPS品种之一;在那些上,您需要使用您的 VPS 的 IP 号码而不是 127.0.0.1。
回答by schmilblick
Please include more information, does $contents contain anything? Remember to do json_decode($contents, true) if you want it as a php array otherwise its a stdClass that gets returned.
请提供更多信息,$contents 是否包含任何内容?如果您希望将其作为 php 数组,请记住执行 json_decode($contents, true) 否则它会返回一个 stdClass 。
Could it have a problem resolving the hostname? is data.mysite.com the same machine as mysite.com?
解析主机名可能有问题吗?data.mysite.com 和 mysite.com 是同一台机器吗?

