php file_get_contents() 未能打开流:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3535799/
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
file_get_contents() failed to open stream:
提问by Marcin
I'm trying to use file_get_contents()
to grab a twitter feed, however I'm getting the following warning:
我正在尝试使用file_get_contents()
抓取 twitter 提要,但是我收到以下警告:
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
My code:
我的代码:
$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name=google&count=6';
$tweets = file_get_contents($feed);
I'm using Google just for the sake of testing. allow_url_fopen
is enabled in my php.ini
file.
我使用 Google 只是为了测试。allow_url_fopen
在我的php.ini
文件中启用。
Any idea what could be wrong?
知道有什么问题吗?
回答by Dejan Marjanovic
You should use the PHP cURLextension if it's available, as it's many times faster, more powerful, and easier to debug. Here is an example that does the same thing you were trying:
您应该使用PHP cURL扩展(如果可用),因为它速度快许多倍、功能更强大且更易于调试。这是一个执行与您尝试相同的操作的示例:
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name=google&count=6';
$tweets = curl($feed);
回答by user2029521
Most probably, your server have mod_security enabled. Add the below lines in your .htaccess after any existing rules and then try to see the issue is still there, after that addition.
最有可能的是,您的服务器启用了 mod_security。在任何现有规则之后在 .htaccess 中添加以下几行,然后尝试在添加之后查看问题仍然存在。
<IfModule mod_security.c>
SecFilterScanPost
</IfModule>