PHP file_get_contents在本地主机上不起作用 [英] PHP file_get_contents does not work on localhost

查看:328
本文介绍了PHP file_get_contents在本地主机上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从OSX上的MAMP服务器的本地主机(http://172.16.65.1/)在我的网站上工作.
我想从Google加载一些JSON,一些简单的测试显示我在这里遇到了问题.

I am working on my website from localhost (http://172.16.65.1/) a MAMP server on OSX.
I want to load some JSON from Google and some simple tests show me I have a problem here..

echo file_get_contents("http://www.google.com"); // FAILS
// PHP log: [07-Dec-2011 23:09:21] PHP Warning:  file_get_contents(http://www.google.com) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: Host is down in /Applications/MAMP/htdocs/-tests/get-json.php on line 3
echo file_get_contents("http://www.yahoo.com"); // FAILS

// echo file_get_contents("http://localhost"); // WORKS
// echo file_get_contents("http://172.16.65.1/"); // WORKS - My MAMP server

对此我该怎么办? 在我的主机提供程序服务器上它可以正常工作.

What can I do about this? It works fine on my host providers server.

推荐答案

摘自 file_get_contents :

如果启用了fopen包装器,则可以将URL用作此功能的文件名.有关如何指定文件名的更多详细信息,请参见fopen().请参阅受支持的协议和包装器,以获取有关各种包装器具有的功能,使用说明以及它们可能提供的任何预定义变量的信息的链接.

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

检入您的php.ini,以便将 allow_url_fopen 设置为on.

我没有注意到您实际上可以在本地使用file_get_contents,所以现在我想这可能与您的firewall settings有关.

I didn't noticed that you actually could use file_get_contents locally, so now I'm thinking that this could have something to do with your firewall settings.

此外,如果尚未完成,请尝试在php.ini中设置user_agent.

Also, try to set the user_agent in your php.ini if not already done.

这篇关于PHP file_get_contents在本地主机上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆