- What PHP, JSON, jQuery and James Cameron have in common
- January 9th, 2010
-
Over the holidays, I spend a few days in Accra. While I was there, one of the things at the top of my todo list was to go and watch James Cameron’s Avatar (which I hear is a fantastic movie, by the way). Only place to watch it of course, was the Silverbird Cinema at the Accra Mall. So, being the consummate geek that I am, I checked their Twitter feed for scheduling information. Turns out Avatar would be showing that evening at 9:30pm. Alright then. Since I would be leaving for Kumasi the next day, that left me just about enough time for me to finish packing, take a nap and have supper first.
Only one tiny hitch; I wasn’t paying attention and misread the time. It was actually showing at 7:30pm. I’m sure you will be able to figure out how the rest of the evening played out. Anyways, fast foward two days later. I’m still grumpy and decided that I would have to do something, even if it as completely useless. So I threw together a little php script that displays the Silverbird movie lineup in a somewhat more reasonable manner.
Basically, this is how it works:
It gets the Silverbird’s last 8 or so twitter status updates in JSON format, applies a regex and writes the results into a list.
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/silverbirdghana.json?callback?");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
I used CURL to retrieve the status data in JSON. It could have easily been in RSS, ATOM or XML but JSON was much much easier to work with. The ‘?callback?’ at the end of the url is required for cross-domain requests(I think).
$output = json_decode($result);
foreach($output as $tweet):
preg_match('/^"(.*)".*\[(.*)\]\s([0-9:]+\s[APM]+).*SCREEN\s([0-9])/', $tweet->text, $info);
$item = '<li>';
$item .= '<span class="title">' .$info[1] . '</span>';
$item .= '<span class="time">' .$info[3] . ' on </span>';
$item .= '<span class="day">' .$info[2] . '</span>';
$item .= '<span class="screen">' .$info[4] . '</span>';
$item .= '</li>';
echo $item;
endforeach;
So using php’s json_decode function to convert the JSON data directly into an associative array, all that was left to do was to loop over the array, apply a regex to the text of the tweets and print the results. Of course, I wasn’t satisfied and ended adding some jQuery flavour to refresh the content dynamically(More on that later. You can have a look at the source code below if you’re curious though). Then I cooked up some CSS and called it a day(meaning I lost interest). Of course, it’s far from perfect: there’s the issue of timeouts and exceeding the Twitter API limits (those are the first two that come to mind).
What was the point of all this? Well I did learn that I am not above doing something completely useless just so that I can write a post telling a story about it. If I have some free time this weekend. I’ll see if I can cook up a desktop widget or something.
Source code: whatson.zip
- Comments (0)
No Comments »
No comments yet.
RSS feed for comments on this post. TrackBack URL
Leave a comment
Ink still drying...
When you're done here
Copyright and all that nonsense.
The content on this blog is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.
Layout based on Changing Man
Valid XHTML and
CSS

