php - How To Obtain All Twitter Followers Without Hitting API Limit -
i imagine it's pretty easy do, can't figure out i'm doing wrong. i'm using abraham's oauth gain access. i'm building database follower's information: screen name, user name , twitter id. nothing special.
i referenced twitter's "cursoring" page, pseudo code, make code. don't want click link see said pesudo code, looks following:
cursor = -1 api_path = "https://api.twitter.com/1.1/endpoint.json?screen_name=targetuser" { url_with_cursor = api_path + "&cursor=" + cursor response_dictionary = perform_http_get_request_for_url( url_with_cursor ) cursor = response_dictionary[ 'next_cursor' ] } while ( cursor != 0 )
with every request, end user gets "cursor" allows them navigate through "pages" of results. each page holds 20, , if have 200 followers have go through 10 pages. have on 900 followers. modified following:
include('config.php'); //db connection include('twitter_oauth.php'); //oauth connection $followers = ""; $cursor = -1; echo '<pre>'; { $consumerkey = 'xxx'; $consumersecret = 'xxx'; $oauthtoken = 'xxx'; $oauthsecret = 'xxx'; $tweet = new twitteroauth($consumerkey, $consumersecret, $oauthtoken, $oauthsecret); $followers = $tweet->get('followers/list', array('screen_name' => 'my_screen_name', 'cursor' => $cursor)); print_r($followers); if (isset($followers->error)) { echo $followers->next_cursor_str; break; } foreach($followers->users $users) { $followersq = mysql_query("select * followers tw_id = '".$users->id."'") or die(mysql_error()); $num_rows = mysql_num_rows($followersq); if ($num_rows == 0) { $followersq2 = "insert followers (screen_name, name, tw_id) values ('".$users->screen_name."', '".$users->name."', '".$users->id."')"; $followersr = mysql_query($followersq2) or die(mysql_error()); echo 'done 1 set<br>'; } } $cursor = $followers->next_cursor_str; } while ( $cursor != 0 ); echo '</pre>'; ?>
the above code calls twitter followers/list , gets first 20 users. gets cursor , goes next one, , repeats. only, seems after 80 users gives me lovely:
[errors] => array ( [0] => stdclass object ( [message] => rate limit exceeded [code] => 88 ) )
i manually next cursor, wait 15 minutes rate limit go down, call function again cursor, next 80 items, key , repeat, want set script can call on , over.
i feel i'm doing wrong, either function call oauth, or outside of somewhere. can point me in right direction?
thank you.
this way faster, there's limitation concerns :
1- make request followers ids ... paging 5000 id in page https://dev.twitter.com/docs/api/1.1/get/followers/ids
2- loop on ids , send each 100 id in comma separated string info https://dev.twitter.com/docs/api/1.1/get/users/lookup
3- u can 1500 user object instead of 300 user object every 15 minutes
but need set timer every 15 request in case followers list more 1500
Comments
Post a Comment