2010-08-21 73 views
13

我试图用file_get_contents()抢Twitter的饲料,但是我得到以下警告:file_get_contents()函数未能打开流:

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request 

我的代码:

$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name=google&count=6'; 
$tweets = file_get_contents($feed); 

我只是为了测试而使用Google。 allow_url_fopen已在我的php.ini文件中启用。

任何想法可能是错误的?

回答

32

如果可用,应该使用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); 
+0

它也更容易调试了很多,因为你可以得到它的转储请求并将其发送头(畸形请求是HTTP 400错误的常见原因) – 2010-08-21 01:49:34

+1

http://stackoverflow.com/questions/ 555523/file-get-contents-vs-curl-what-has-better-performance – 2010-08-21 02:02:41

+0

是的,大约快4-5倍。 – 2010-08-21 02:12:23

0

最有可能的是,你的服务器启用了mod_security。在任何现有规则之后,在.htaccess中添加下面的行,然后在添加之后尝试查看问题仍然存在。

<IfModule mod_security.c> 
SecFilterScanPost 
</IfModule> 
相关问题