FFi*_*ish 9 php localhost echo file-get-contents
我正在我的网站上从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
Run Code Online (Sandbox Code Playgroud)
我该怎么办?它在我的主机提供商服务器上工作正常.
小智 11
您还需要签入PHP.ini文件
extension = php_openssl.dll
Run Code Online (Sandbox Code Playgroud)
是否启用,如果没有,则通过删除启用; 标志
allow_url_fopen = on
Run Code Online (Sandbox Code Playgroud)
从以下文档file_get_contents:
如果已启用fopen包装器,则URL可用作此函数的文件名.有关如何指定文件名的更多详细信息,请参阅fopen().请参阅支持的协议和包装器,以获取有关各种包装器具有哪些功能的信息的链接,有关其使用的说明以及它们可能提供的任何预定义变量的信息.
检查您php.ini这样allow_url_fopen设置为on.
编辑:
我没注意到你实际上可以file_get_contents在本地使用,所以现在我认为这可能与你的有关firewall settings.
另外,尝试设置user_agent你的php.iniif if not already.
尝试用这个函数代替 file_get_contents():
<?php
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Run Code Online (Sandbox Code Playgroud)
它可以像 file_get_contents() 一样使用,但使用 cURL。
在 Ubuntu(或其他具有 aptitude 的类 UNIX 操作系统)上安装 cURL:
sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart
Run Code Online (Sandbox Code Playgroud)
另请参见卷曲
| 归档时间: |
|
| 查看次数: |
49671 次 |
| 最近记录: |