开发者

Twitter API: Get Followers +99

Using the twitter API (and OAuth) if i was to call for the user follower开发者_StackOverflow社区s, (statuses/followers) i would be returned only 99 results.

Is there a way i can return 99, then call again starting at follower 100 then looping through this style of calling until the total number of followers has been returned?

Or just return ALL followers?


You need to specify cursor parameter as described in the API documrnation. E.g. specify cursor=-1 to request the first page and then use a next_cursor value returned in the first response:

  http://twitter.com/statuses/followers/barackobama.xml?cursor=-1
  http://twitter.com/statuses/followers/barackobama.xml?cursor=1300794057949944903


<?php
$trends_url = "http://api.twitter.com/1/statuses/followers/fawadghafoor.json";
$ch       = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $trends_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout  = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);

foreach($response as $friends){
      $thumb = $friends['profile_image_url'];
      $url   = $friends['screen_name'];
      $name  = $friends['name'];
?>                         
<a title="<?php echo $name;?>" href="http://www.twitter.com/<?php echo $url;?>"><img class="photo-img" src="<?php echo $thumb?>" border="0" alt="" width="40" /></a>
    <?php  }  ?>


Be sure you're using the right call. followers/ids gives you 5000 at a time (but it's just a list of ids). This call, too, uses the cursor to let you step through pages of users. You get a zero back when you have them all.


Twitter API restricts us to make api Call for method followers/ids is 15 requests per 15 minutes. If you make more than this api will give you an error message that Rate Limit Reached.

For more information of twitter API rate Limit Visit-https://dev.twitter.com/docs/rate-limiting/1.1 and https://dev.twitter.com/docs/rate-limiting/1.1/limits


Twitter only allows a certain number of API requests per hour, and I think minute. You might not be able to retrieve any more than 99 requests at once.


Though I asked this quite a while ago, I came back to building something quite similar recently (+ new programming skills).

I noticed Twitter API have a method to get all of a users followers (or followings) user ID's in one request. I found the best way was to array_chunk up the ID's into batches of 100 (and only take the first 30 arrays as I dont want to use all the users api requests that hour - they might want to actually tweet!). Then there is a method that allows you to get up to 100 users userinfo (from the view of the currently authenticated user) so I just do a loop (sleep a bit inbetween) and then you've got 30,000 twitter followers!

I would recommend doing this asynchronously in a queue system, as if you do this on the fly when the users requests a page on the site it could be very slow and you might be prone to a HTTP timeout. Also cache them like hell!

Sorry I didn't post any code, but hopefully this thought process will help someone :)


$cursor = -1;
$account_from = 'twitter_account';
do
{
    $json = file_get_contents('http://api.twitter.com/1/statuses/followers/' . $account_from .'json?cursor=' . $cursor);
    $accounts = json_decode($json);
    foreach ($accounts->users as $account)
    {

            array(
                ':twitter_id' => $account->id_str,
                ':account' => $account->screen_name,
                ':description' => $account->description,
            );
    }
    $cursor = $accounts->next_cursor;

}
while ($cursor > 0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜