So far so good. I started with the composer package abraham/twitteroauth .
The most popular PHP library for use with the Twitter OAuth REST API.
It's an easy to use library when you want to interact with Twitter's Rest API.
All what you need is a registered App on Twitter.
You can create a folder somewhere and type
composer require abraham/twitteroauth.
Now create a file like
follower.php and write this:
$api-> gives you now a lot of methods to interact with the Twitter's REST Api.
A list of my followers
I searched in Twitter's REST API documentation and the only thing I found was the the possibility to get a list of IDs who follow me. GET followers/ids.
Returns a cursored collection of user IDs for every user following the specified user. At this time, results are ordered with the most recent following first — however, this ordering is subject to unannounced change and eventual consistency issues. Results are given in groups of 5,000 user IDs and multiple "pages" of results can be navigated through using the next_cursor value in subsequent requests. See Using cursors to navigate collections for more information.
This method is especially powerful when used in conjunction with GET users / lookup, a method that allows you to convert user IDs into full user objects in bulk.
That looks good. With the list of the IDs I can use another endpoint to fetch the correct usernames to the IDs.
I don't have 5000 followers, so I can ignore the cursored stuff.
Fetch all IDs who follow me
It works. I get a response with a long list of IDs who follow me. Now I try to call the other endpoint to get the correct usernames because it's easier to read
The GET followers/ids
documentation gave me already the hint to
use GET users/lookup.
Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters. This method is especially useful when used in conjunction with collections of user IDs returned from GET friends / ids and GET followers / ids.
Ok, that means I can send up to 100 IDs per request. I chunk the array of IDs I got from GET followers/ids with PHP's
array_chunk() method and use
implode() to generate a comma seperated list of IDs.
Now I have two posibilities to wrote this in a text file.
I can pipe the output into a file with
php follower.php > follower.txt
or I do it in the script
Everytime we execute the script now,
follower.txt will be updated and we can use
git diff to see the changes.
The whole script
I'm a huge fan of Laravel's collections.
To refactor this to collections, all we need is tightenco/collect.
composer require tightenco/collect
follower.txtin a github repo and do whatever you want :)