PHP file_get_contents 在本地主机上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8423404/
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 does not work on localhost
提问by FFish
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..
我正在从 localhost (http://172.16.65.1/) 一个 OSX 上的 MAMP 服务器在我的网站上工作。
我想从 Google 加载一些 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
What can I do about this? It works fine on my host providers server.
我该怎么办?它在我的主机提供商服务器上运行良好。
采纳答案by Cyclonecode
From the documentation for file_get_contents
:
从文档中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.
如果已启用 fopen 包装器,则可以将 URL 用作此函数的文件名。有关如何指定文件名的更多详细信息,请参阅 fopen()。请参阅支持的协议和包装器以获取有关各种包装器具有哪些功能、使用说明以及它们可能提供的任何预定义变量的信息的链接。
Check in your php.ini
so allow_url_fopen
is set to on
.
签入您的php.ini
soallow_url_fopen
设置为on
.
EDIT:
编辑:
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
.
我没有注意到您实际上可以file_get_contents
在本地使用,所以现在我认为这可能与您的firewall settings
.
Also, try to set the user_agent
in your php.ini
if not already done.
另外,如果尚未完成,请尝试user_agent
在您的设置中设置php.ini
。
回答by Niranjan
You need to also check in PHP.ini
file
您还需要签入PHP.ini
文件
extension = php_openssl.dll
is enable or not, if not then just enable that by removing ; sign
是否启用,如果不是,则通过删除启用它;标志
allow_url_fopen = on
回答by dchrastil
This may be a setting in your php.ini file. There is a setting for allow_url_fopen which enables/disables the ability to open remote files from php. For security reasons this is usually defaulted to disabled. You can enable it in your php.ini by adding the following line:
这可能是 php.ini 文件中的一个设置。allow_url_fopen 有一个设置,它启用/禁用从 php 打开远程文件的能力。出于安全原因,这通常默认为禁用。您可以通过添加以下行在 php.ini 中启用它:
allow_url_fopen = 1
Again, be aware of the security concerns when using this feature.
同样,使用此功能时请注意安全问题。
回答by Adelmar
Try this function in place of file_get_contents():
试试这个函数来代替 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;
}
It can be used just like file_get_contents(), but uses cURL.
它可以像 file_get_contents() 一样使用,但使用 cURL。
Install cURL on Ubuntu (or other unix-like operating system with aptitude):
在 Ubuntu(或其他具有 aptitude 的类 Unix 操作系统)上安装 cURL:
sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart
See also cURL
另见卷曲
回答by CodeCaster
In the second one you're trying to open a file named localhost in the current folder, which doesn't exist and hence throws an error. Use http://localhostinstead. And to make that work, you're have to set allow_furl_open.
在第二个中,您尝试在当前文件夹中打开一个名为 localhost 的文件,该文件不存在并因此引发错误。请改用http://localhost。为了使这项工作,你必须设置allow_furl_open。
回答by karambir
I was using file_get_contentsthis way: file_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=".$screenname."&count=5");
我以这种方式使用file_get_contents: file_get_contents(" https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=".$screenname."&count=5");
And that was not working, so I changed https to http and after that it is working well.
这不起作用,所以我将 https 更改为 http,之后它运行良好。
file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=".$screenname."&count=5");
回答by Lyubomir Parvanov
For me the problem was that file_get_contents
didn't work on https://domain.test.loc(domain that resolves to localhost), but worked on http://domain.test.loc. Maybe it doesn't like the self-signed certificate. I do have allow_url_fopen
set to ON and extension = php_openssl.dll.
对我来说,问题是file_get_contents
在https://domain.test.loc(解析为 localhost 的域)上不起作用,但在http://domain.test.loc上起作用。也许它不喜欢自签名证书。我确实allow_url_fopen
设置为 ON 和 extension = php_openssl.dll。