Twitter API always saying 400 Bad Request

送分小仙女□ 提交于 2020-01-03 04:48:12

问题


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;

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

Warning: fopen(http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Schodemeiss&count=5) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/ellexus1/public_html/settings.php on line 187

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

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?

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!

Thanks.

Andrew

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.


回答1:


Chances are you are getting rate-limited

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 requests per hour for non authenticated calls (Based on IP-addressing)

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

You have to authenticate to avoid these errors popping up.

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.

Replace the fopen with

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



回答2:


This may help

Error codes

https://developer.twitter.com/en/docs/basics/response-codes.html

Error codes defination is given in above link



来源:https://stackoverflow.com/questions/7835067/twitter-api-always-saying-400-bad-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!