How to get a complete list of Twitter subscribers using the new API 1.1

I use this https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=sitestreams&count=5000 to list Twitter followers, but I only have a list of 200 followers. How to grow Twitter followers with the new API 1.1?

+4
source share
3 answers

You must first configure the application

 <?php $consumerKey = 'Consumer-Key'; $consumerSecret = 'Consumer-Secret'; $oAuthToken = 'OAuthToken'; $oAuthSecret = 'OAuth Secret'; # API OAuth require_once('twitteroauth.php'); $tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret); 

You can download twitteroauth.php here: https://github.com/elpeter/pv-auto-tweets/blob/master/twitteroauth.php

Then

You can find your followers as follows:

 $tweet->get('followers/ids', array('screen_name' => 'YOUR-SCREEN-NAME-USER')); 

If you want to get the next group of 5,000 followers, you must add the cursor value from the first call.

 $tweet->get('followers/ids', array('screen_name' => 'YOUR-SCREEN-NAME-USER', 'cursor' => 9999999999)); 

You can read: Using cursors to navigate the collection at this link: https://dev.twitter.com/docs/misc/cursoring

+4
source

You cannot get more than 200 at the same time ... It was clearly indicated in the documentation where count:

The number of users to return to the page, up to a maximum of 200. The default is 20.

you can somehow do this through pagination using

"cursor = -1" #means p. 1, "If no cursor is specified, it assumes a value of -1, which is the first" page ".

0
source

This is how I run / update the full list of Repeater IDs on my platform. I would avoid using sleep() as an @aphoe script. Really bad to keep in touch for so long, and what happens if your user has 1MILL followers? Are you going to keep this connection open for a week? lol If you need, run cron or save the redis / memcache command. Rinse and repeat until you get all the followers.

Please note: my code below is a class that runs through the cron command every minute. I am using Laravel 5.1. Therefore, you can probably ignore many of this code, as it is unique to my platform. For example, TwitterOAuth (which gets all oAuths from me on db), TwitterFollowerList is another table, and I check if the record exists, TwitterFollowersDaily is another table in which I store / update the total amount per day for the user and TwitterApi - this is the Abraham\TwitterOAuth . However, you can use any library.

This may give you a good idea of ​​what you can do the same or even better. I will not explain the whole code because there is a lot going on, but you should be able to navigate through it. Let me know if you have any questions.

 /** * Update follower list for each oAuth * * @return response */ public function updateFollowers() { TwitterOAuth::chunk(200, function ($oauths) { foreach ($oauths as $oauth) { $page_id = $oauth->page_id; $follower_list = TwitterFollowerList::where('page_id', $page_id)->first(); if (!$follower_list || $follower_list->updated_at < Carbon::now()->subMinutes(15)) { $next_cursor = isset($follower_list->next_cursor) ? $follower_list->next_cursor : -1; $ids = isset($follower_list->follower_ids) ? $follower_list->follower_ids : []; $twitter = new TwitterApi($oauth->oauth_token, $oauth->oauth_token_secret); $results = $twitter->get("followers/ids", ["user_id" => $page_id, "cursor" => $next_cursor]); if (isset($results->errors)) continue; $ids = $results->ids; if ($results->next_cursor !== 0) { $ticks = 0; do { if ($ticks === 13) { $ticks = 0; break; } $ticks++; $results = $twitter->get("followers/ids", ["user_id" => $page_id, "cursor" => $results->next_cursor]); if (!$results) break; $more_ids = $results->ids; $ids = array_merge($ids, $more_ids); } while ($results->next_cursor > 0); } $stats = [ 'page_id' => $page_id, 'follower_count' => count($ids), 'follower_ids' => $ids, 'next_cursor' => ($results->next_cursor > 0) ? $results->next_cursor : null, 'updated_at' => Carbon::now() ]; TwitterFollowerList::updateOrCreate(['page_id' => $page_id], $stats); TwitterFollowersDaily::updateOrCreate([ 'page_id' => $page_id, 'date' => Carbon::now()->toDateString() ], [ 'page_id' => $page_id, 'date' => Carbon::now()->toDateString(), 'follower_count' => count($ids), ] ); continue; } } }); } 
0
source

Source: https://habr.com/ru/post/1489202/


All Articles