file_get_contents()无法打开流:

Mar*_*cin 13 php

我正试图用来file_get_contents()获取推特,但是我收到了以下警告:

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
Run Code Online (Sandbox Code Playgroud)

我的代码:

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

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

知道什么可能是错的吗?

Dej*_*vic 33

您应该使用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);
Run Code Online (Sandbox Code Playgroud)