I Wrote a Twitter Widget
Jul. 19th, 2009 07:01 amFor the last couple days, due to feeling kinda blah, and being delayed on a pending project (*pokes a certain fox*), I've been playing around with displaying my Twitter feed on my website (among other places). I'd been eagerly wanting to replace the Flash version provided by Twitter with something not Flash-based, but there seems to be a shortage of Twitter website widgets that are easy to re-theme to match the website they're on, and aren't tied into some other service or application. But, at one point, it looked like I found what I was looking for, TwPHPer. It does its job fairly well, doesn't require Javascript, and is overall a decent widget. I recommend it to anyone looking for a simple way to put their Twitter feed on their website without it looking grossly out of place.
However, I wanted more. TwPHPer will always be limited by the fact that it uses the Twitter RSS feed to retrieve data instead of the main API. It's inherently slower than an API-based script, and can display only basic data. What I've been looking for, and haven't found, is a widget that will display as much detail as the main website does, including designating replies and showing the source application each tweet came from (call me a nerd, but I love looking at that info). When my searching didn't turn up anything that met that criteria, I did what any good programmer would do, and wrote my own.
Since I didn't set out to write a full API interface, I tried searching around for a good one that was already written. Again, not many helpful results, but I saw that the Zend Framework (collection of scripts provided by the people who wrote PHP itself) had a very nice one, so I went with that. Long story short, I finished it today, and I'm quite proud of the finished product.
In addition to a significant speed improvement over TwPHPer (roughly 1/2 to 1/6 the script execution time, depending on whether data is cached), my new widget has the following features:
-Data caching, to improve performance and prevent API limit overruns.
-Reply indicator, with link to parent tweet.
-Location indicator, based on client used (can also easily be adapted to display the raw source app info). This is specific to me, obviously, but quite handy for my use.
-Direct link to each tweet, so logged-in users can easily reply, and conversations can more easily be followed.
After getting it working on my website, I also wrote a version that can be embedded using an <object> tag, so I can use it on sites like LJ, Dreamwidth, and *gags* F4L. It doesn't use Flash or anything, it's just a blank webpage with the widget that can be embedded with this code:
<object type="text/html" data="http://www.lupinia.eu/INC/twitter_embed.php"></object>
I did borrow some code from TwPHPer, namely its link/username parsers, and the spiffy scrollbar replacement (requires JS, but doesn't affect functionality), but aside from that, this is all mine :-)
I don't plan to distribute this, since it's very custom-written for my use, but if anyone wants to see the code, I'll probably send it to you, all you have to do is ask :-)