I thought it’d be interesting to write a script to keep track of my Twitter followers, and notify me if someone stops following me. There are several existing sites which can do this for you, but I’m a bit wary about signing into random sites with my Twitter account (some of them will spam messages to your feed), and I also wanted to dust off my PHP coding skills so here was a decent opportunity.
The Twitter API provides a method to retrieve your follower list – the URL for my follower list is:
You can specify the format of the list by changing the extension – I went for JSON, but you can also choose XML, RSS or Atom.
To begin with, as a one-off, I retrieved my follower list and stored it in a MySQL database. Then, I coded a script to do the following:
- Retrieve the follower list stored in the MySQL database, and store in an array called
- Retrieve the most recent follower list and store in an array called
- If there are any new followers, retrieve their username and screen name using the Twitter API.
$newFollowers– if there is a follower in
$oldFollowersthat doesn’t appear in
$newFollowersthen that person no longer follows me.
- If there are any people who no longer follow me, send an email alert.
I’ve added a cron job to run this once a day. So far there haven’t been any problems and it’s running smoothly.
Update 5th August 2011: I’ve created a repository on github with the script and some SQL to define the required table. I mentioned above about the script sending an email alert, but in reality all it does is echo a message saying ‘xxx is no longer following you’, which is then emailed to me as the output of the cron job.
Update 13th March 2013: I’ve just committed a fix to the script – it was broken by a Twitter API update last year but I’ve only just got around to updating the script on github. It should be working now!