Twitter API 总是说 400 Bad Request [英] Twitter API always saying 400 Bad Request

查看:33
本文介绍了Twitter API 总是说 400 Bad Request的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the following code to retrieve an amount of Tweets from the 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,因为我收到错误:

Of course $username and the other variables inside the fopen request are correct and it produces the correct URL because I get the error:

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

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

that ^^ error returns whenever I try and open my page.

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

Any ideas why this might be? Do I need to use OAuth to even just get my tweets!? Do I have to register my website as somewhere that might get posts?

我真的不知道为什么会这样.我的主机是 JustHost.com,但我不确定这是否有任何区别.欢迎所有想法!

I'm really not sure why this is happening. My host is JustHost.com, but I'm not sure if that makes any diffrence. All ideas are welcome!

谢谢.

安德鲁

附注.这段代码位于一个函数中,用户名、间隔和计数被正确传入,因此在错误代码中它创建了一个格式正确的地址.

PS. This code lies inside a function where username, interval and count are passed in correctly, hence in the error code its created a well formed address.

推荐答案

您可能会受到速率限制

400 错误请求:请求无效.伴随的错误消息将解释原因.这是将返回的状态码限速期间.

400 Bad Request: The request was invalid. An accompanying error message will explain why. This is the status code will be returned during rate limiting.

未经身份验证的呼叫每小时 150 个请求(基于 IP 寻址)

每小时 350 次已验证调用请求(基于已验证用户调用)

350 requests per hour for authenticated calls (Based on the authenticated users calls)

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

You have to authenticate to avoid these errors popping up.

并且在处理 twitter 时也请使用 cURL.我用file_get_contentsfopen 调用了twitter API,发现很不靠谱.你会时不时地受到打击.

And also please use cURL when dealing with twitter. I've used file_get_contents and fopen to call the twitter API, and found that it is very unreliable. You would get hit with that every now and then.

$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);

这篇关于Twitter API 总是说 400 Bad Request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆