php PHP5给出无法打开流:使用fopen时HTTP请求失败错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4608788/
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
PHP5 giving failed to open stream: HTTP request failed error when using fopen
提问by mickey
This problem seems to have been discussed in the past everywhere on google and here, but I have yet to find a solution.
过去似乎在 google 和 here 上到处都讨论过这个问题,但我还没有找到解决方案。
A very simple fopen gives me a
一个非常简单的 fopen 给了我一个
PHP Warning: fopen(http://www.google.ca): failed to open stream: HTTP request failed!".
PHP 警告:fopen( http://www.google.ca):无法打开流:HTTP 请求失败!”。
The URL I am fetching have no importance because even when I fetch http://www.google.comit doesnt work. The exact same script works on different server. The one failing is Ubuntu 10.04 and PHP 5.3.2. This is not a problem in my script, it's something different in my server or it might be a bug in PHP.
我获取的 URL 并不重要,因为即使我获取http://www.google.com它也不起作用。完全相同的脚本适用于不同的服务器。失败的一个是 Ubuntu 10.04 和 PHP 5.3.2。这在我的脚本中不是问题,它在我的服务器中有所不同,或者它可能是 PHP 中的错误。
I have tried using a user_agent in php.ini but no success. My allow_url_fopen is set to On.
我曾尝试在 php.ini 中使用 user_agent 但没有成功。我的 allow_url_fopen 设置为 On。
If you have any ideas, feel free!
如果您有任何想法,请随意!
回答by Michael Irigoyen
It sounds like your configuration isn't allowed to use file functions, which is common these days because of security concerns. If you have the cURL libraries available to you, I would recommend trying those out.
听起来您的配置不允许使用文件功能,这在当今很常见,因为出于安全考虑。如果您有可用的 cURL 库,我建议您尝试一下。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.ca/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
echo $file;
回答by Andy Baird
Check that your php.ini config is set to allow fopen to open external URL's:
检查您的 php.ini 配置是否设置为允许 fopen 打开外部 URL:
allow_url_fopen "1"
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
回答by Adam
I'm not at all sure about whether this is the problem or not, but I know in the past I've had problems with opening URLs with fopen, often due to php.ini's allow_url_fopen or other unknown security settings
我完全不确定这是否是问题所在,但我知道过去我在使用 fopen 打开 URL 时遇到问题,通常是由于 php.ini 的 allow_url_fopen 或其他未知的安全设置
You may want to try cURLin PHP, which often works for me, you'll find an example really easily by googling that.
您可能想在 PHP 中尝试cURL,这通常对我有用,您可以通过谷歌搜索轻松找到一个示例。
回答by German Rumm
Check your phpinfo
output - is http
present under Registered PHP Streams
?
检查您的phpinfo
输出 - 是否http
存在于Registered PHP Streams
?
回答by marcovtwout
Are you getting "HTTP request failed" without further details? The socket timeout could be expired. This default to 60 seconds. See: http://php.net/manual/en/function.socket-set-timeout.php
您是否在没有更多详细信息的情况下收到“HTTP 请求失败”?套接字超时可能已过期。这默认为 60 秒。见:http: //php.net/manual/en/function.socket-set-timeout.php