PHP file_get_contents 在本地主机上不起作用

发布于 2024-12-19 20:54:56 字数 692 浏览 3 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

口干舌燥 2024-12-26 20:54:56

您还需要检查 PHP.ini 文件

extension = php_openssl.dll

是否启用,如果没有,则只需通过删除来启用它;符号

allow_url_fopen = on

You need to also check in PHP.ini file

extension = php_openssl.dll

is enable or not, if not then just enable that by removing ; sign

allow_url_fopen = on
闻呓 2024-12-26 20:54:56

file_get_contents 的文档中:

如果已启用 fopen 包装器,则可以使用此函数将 URL 用作文件名。有关如何指定文件名的更多详细信息,请参阅 fopen()。请参阅支持的协议和包装器,获取有关各种包装器具有哪些功能的信息、有关其用法的注释以及有关它们可能提供的任何预定义变量的信息的链接。

检查您的 php.ini 以便 allow_url_fopen 设置为on

编辑:

我没有注意到你实际上可以在本地使用file_get_contents,所以现在我认为这可能与您的防火墙设置

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

From the documentation for 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.

Check in your php.ini so allow_url_fopen is set to 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.

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

云淡月浅 2024-12-26 20:54:56

尝试用此函数代替 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;
}

它可以像 file_get_contents() 一样使用,但使用 cURL。

在 Ubuntu(或其他具有 aptitude 的类 UNIX 操作系统)上安装 cURL:

sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart

另请参阅 cURL

Try this function in place of 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.

Install cURL on Ubuntu (or other unix-like operating system with aptitude):

sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart

See also cURL

一曲爱恨情仇 2024-12-26 20:54:56

这可能是 php.ini 文件中的设置。 allow_url_fopen 有一个设置,可以启用/禁用从 php 打开远程文件的功能。出于安全原因,这通常默认为禁用。您可以通过添加以下行在 php.ini 中启用它:

allow_url_fopen = 1

再次强调,使用此功能时请注意安全问题。

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:

allow_url_fopen = 1

Again, be aware of the security concerns when using this feature.

http://php.net/manual/en/filesystem.configuration.php

深空失忆 2024-12-26 20:54:56

对我来说,问题是 file_get_contentshttps://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 have allow_url_fopen set to ON and extension = php_openssl.dll.

尸血腥色 2024-12-26 20:54:56

在第二个中,您尝试在当前文件夹中打开一个名为 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.

红颜悴 2024-12-26 20:54:56

我以这种方式使用 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,之后它运行良好。

file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=".$screenname."&count=5");

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.

file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=".$screenname."&count=5");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文