Php Twitter API总是说400个错误请求

Php Twitter API总是说400个错误请求,php,api,twitter,Php,Api,Twitter,我正在使用以下代码从Twitter API检索大量推文: $cache_file = "cache/$username-twitter.cache"; $last = filemtime($cache_file); $now = time(); $interval = $interval * 60; // ten minutes // Check the cache file age if ( !$last || (( $now - $last ) > $interval) ) {

我正在使用以下代码从Twitter API检索大量推文:

$cache_file = "cache/$username-twitter.cache";
$last = filemtime($cache_file);
$now = time();
$interval = $interval * 60; // ten minutes

// Check the cache file age
if ( !$last || (( $now - $last ) > $interval) ) {
    // cache file doesn't exist, or is old, so refresh it

    // Get the data from Twitter JSON API
    //$json = @file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" . $username . "&count=" . $count, "rb");
    $twitterHandle = fopen("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$count", "rb");
    $json  = stream_get_contents($twitterHandle);
    fclose($twitterHandle);

    if($json) {
        // Decode JSON into array
        $data = json_decode($json, true);
        $data = serialize($data);

        // Store the data in a cache
        $cacheHandle = fopen($cache_file, 'w');
        fwrite($cacheHandle, $data);
        fclose($cacheHandle);
    }

}

// read from the cache file with either new data or the old cache
$tweets = @unserialize(file_get_contents($cache_file));

return $tweets;
当然,$username和fopen请求中的其他变量是正确的,它生成了正确的URL,因为我得到了错误:

警告:fopen(http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Schodemeiss&count=5)[function.fopen]:无法打开流:HTTP请求失败!HTTP/1.1 400第187行/home/ellexus1/public_html/settings.php中的错误请求

每当我尝试打开页面时,都会返回该^^错误

你知道为什么会这样吗?我是否需要使用OAuth来获取我的推文!?我是否必须注册我的网站作为可能获得帖子的地方

我真的不知道为什么会这样。我的主机是JustHost.com,但我不确定这是否有什么不同。欢迎提出任何意见

谢谢

安德鲁


注:此代码位于正确传入用户名、间隔和计数的函数内,因此在错误代码中创建了格式良好的地址。

您可能会受到速率限制

400错误请求:请求无效。伴随的错误 这条信息将解释原因。这是将返回的状态代码 在速率限制期间

非认证呼叫每小时150次请求(基于IP地址)

每小时350次认证呼叫请求(基于认证用户呼叫)

您必须进行身份验证以避免出现这些错误

在处理twitter时也请使用
cURL
。我使用
file\u get\u contents
fopen
调用twitter API,发现它非常不可靠。你会时不时地被它击中

fopen
替换为

$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$count");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$it = curl_exec($ch); //content stored in $it
curl_close($ch);
这可能会有帮助

Error codes


错误代码定义在上面的链接中给出

奇怪,它适用于我
curl-D-http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Schodemeiss&count=5“