Exposing the Twitter XML API with PHP
Recently Robert Scoble was complaining about how hard it was to add all of his Twitter followers as friends automatically. I figured it should be pretty easy to script. Well, it should be, but Twitter makes it unnecessarily complicated.
First, the Twitter API is marginally useful, but doesn’t let you actually do much besides passively read and post updates. I used the followers list XML and created a PHP script that snarfed the XML and put it in an associative array. My intent was to create a new followers page where you could check a box next to each follower (with a handy “Select All” button as well) and one-click to add those followers as friends.
Unfortunately, Twitter does not expose the login portion of the API, and just going to http://twitter.com/friendships/create/[follower ID number] without first going to the friend’s page redirects you back to your Twitter home page — even when you’re already logged in to Twitter! This is really broken — the Twitter API should let you work with their URLs.
Since Twitter’s API is completely useless for adding friends, and their interface doesn’t seem to allow for going directly to a URL to add a friend, I am stuck. However, I am happy to release the PHP code I wrote so that others can play around with Twitter’s API.
I grabbed the XML parser from a page on php.net — this is actually very useful to parse ANY XML document into a multi-dimensional associative array in PHP. My source code works. Right now it takes your username and password, sends that to Twitter, gets a list of your followers and displays them in a table, along with the correct link to add that friend (which, of course, doesn’t work because Twitter won’t accept it.)
It is my hope that Twitter will make their URLs and API much more useful in the future so that this sort of thing becomes easy. My script goes a long way toward making that possible on the client side, at least.
My source code
The XML parser I used
See the script in action! (Enter your Twitter username and password to get a list of your followers.)
This script is free to use, borrow, tweak, and hopefully will spur the Twitter developers into action and make them realize the importance of creating a better API.