TwitterTronix
Want to know why Web 2.0 is sooo much better than Web 1.0? It’s because I just talked to Jack from Twitter ON THE GODDAMN TELEPHONE, and he’s says they are going to implement a callback param for their web API, just because I asked :)
Web APIs that return JSON are great, but not all browsers fire onload events for script elements, so if you want to load the JSON asynchronously/dynamically/on-demand, you need to think of something else.
One good way of doing this is wrapping the returned JSON in a callback function, which is best done by adding a callback param to the API, as shown below:
http://twitter.com/t/status/user_timeline/6760?count=1&callback=myCallback
This returns the exact same JSON, just wrapped in a call to your callback function:
myCallback([{"user": {"name": "rajbot", "id": 6760, "screen_name": "rajbot"}, "text": "enjoying the second summer :)", "id": 35084, "relative_created_at": "about 3 hours ago", "created_at": "Mon Sep 25 22:20:49 UTC 2006"}]);
Very simple, and it works in all browsers, so it’s wonderful that Twitter is going to update their API. But what if you are impatient like us and want to use this technique RIGHT NOW? Well, if you don’t care about error checking, you can wrap the existing Web API with three lines of your favorite scripting language:
<?php $id = $_REQUEST['id']; $result = file_get_contents("http://twitter.com/t/status/user_timeline/{$id}?count=1"); echo "twitterCallback($result);"; ?>
We now display the twitter status in the header. Since we call the Twitter Web API after the entire page finished loading, our readers no longer have to wait for the Twitter servers to respond before reading our babble. We expect this bump up global productivity by several minutes!
Update: Twitter has implemented callbacks! That was fast :)
Filed under: code code |
Tagged: javascript
