PHP file_get_contents 在本地主机上不起作用
我正在从 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
我该怎么办? 它在我的主机提供商服务器上运行良好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您还需要检查 PHP.ini 文件
是否启用,如果没有,则只需通过删除来启用它;符号
You need to also check in
PHP.ini
fileis enable or not, if not then just enable that by removing ; sign
从
file_get_contents
的文档中:检查您的
php.ini
以便allow_url_fopen
设置为on
。编辑:
我没有注意到你实际上可以在本地使用
file_get_contents
,所以现在我认为这可能与您的防火墙设置
。另外,如果尚未完成,请尝试在
php.ini
中设置user_agent
。From the documentation for
file_get_contents
:Check in your
php.ini
soallow_url_fopen
is set toon
.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 yourfirewall settings
.Also, try to set the
user_agent
in yourphp.ini
if not already done.尝试用此函数代替 file_get_contents():
它可以像 file_get_contents() 一样使用,但使用 cURL。
在 Ubuntu(或其他具有 aptitude 的类 UNIX 操作系统)上安装 cURL:
另请参阅 cURL
Try this function in place of file_get_contents():
It can be used just like file_get_contents(), but uses cURL.
Install cURL on Ubuntu (or other unix-like operating system with aptitude):
See also cURL
这可能是 php.ini 文件中的设置。 allow_url_fopen 有一个设置,可以启用/禁用从 php 打开远程文件的功能。出于安全原因,这通常默认为禁用。您可以通过添加以下行在 php.ini 中启用它:
再次强调,使用此功能时请注意安全问题。
http://php.net/manual/en/filesystem.configuration.php
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:
Again, be aware of the security concerns when using this feature.
http://php.net/manual/en/filesystem.configuration.php
对我来说,问题是
file_get_contents
在 https://domain.test.loc 上不起作用 (解析为 localhost 的域),但在 http://domain.test.loc 上工作。也许它不喜欢自签名证书。我确实将allow_url_fopen
设置为 ON 并将扩展名设置为 php_openssl.dll。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 haveallow_url_fopen
set to ON and extension = php_openssl.dll.在第二个中,您尝试在当前文件夹中打开一个名为 localhost 的文件,该文件不存在,因此会引发错误。使用 http://localhost 代替。为了使其工作,你必须设置allow_furl_open。
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://localhost instead. And to make that work, you're have to set allow_furl_open.
我以这种方式使用 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" );
这不起作用,所以我将 https 更改为 http,之后它运行良好。
I was using file_get_contents this way:
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.