微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP file_get_contents不适用于localhost

我正在我的网站上从localhost(http://172.16.65.1/)在OSX上运行MAMP服务器.
我想从谷歌加载一些JSON,一些简单的测试告诉我这里有一个问题..

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

我该怎么办?
它在我的主机提供商服务器上工作正常.

解决方法:

file_get_contents的文档:

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,所以现在我认为这可能与你的防火墙设置有关.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐