Cross Site Status Updater

This post assumes semi-advanced understanding of PHP scripting as well as basic understanding of Twitter and Facebook.

When I started working on launching my personal website, an interesting problem came up. I wanted to have a feature that showed a current status on my site, similar to a Facebook status. Also, since I also have a Twitter and Facebook account, it only made sense to have a single place to update my Twitter, Facebook, and personal site status.

After doing a bit of research, I decided to use Twitter as the central updater. Twitter has many nice applications built into it, including the ability to update from a desktop or phone. Once my Twitter status was updated, the change would get carried down to my Facebook account and personal site through the Twitter feed.

The first step was with Facebook, and ended up being quite easy. There is already an application built for this that works quite well. Once you agree to use the application, all you need to do is sign into your Twitter account through Facebook and change a few settings, and every tweet you make is imported to your Facebook status.

The next step was updating my own site. I chose to use a PHP script for this, utilizing the Twitter feed generated by my updates - since my screen name is jpemeric, this feed is at http://twitter.com/statuses/user_timeline.xml?screen_name=jpemeric. To take it a step further, I wanted to save my Twitter updates in a local table. This not only allowed me to update my site when a new tweet had been made, but also saved a copy of all the status updates for my own records.

Here's the script I used.

  1. $url = 'http://twitter.com/statuses/user_timeline.xml?screen_name=jpemeric';

  2. //link to my twitter feed

  3. $array = simplexml_load_file($url);

  4. //pulls my twitter feed and turns it into an array

  5. $latest_update = sql("select max(timestamp) from twitters",n);

  6. //finds out when my last tweet was, from a custom sql function

  7. if($array) {

  8. foreach($array->status as $status) {

  9. $time = strtotime($status->created_at);

  10. //finds status update time

  11. if($time>$latest_update['max(timestamp)']) {

  12. //if status is newer then the last tweet in the table

  13. if($status->in_reply_to_user_id=='') {

  14. //makes sure it's not a tweetback

  15. sql("insert into twitters values('$time','jpemeric','$status->text')");

  16. }

  17. }

  18. }

  19. }

The only problem I've found with this script is the occasional downtime of Twitter - the feed is not always online, resulting in a file not found error.

To display your status, all you need to do at this point is do an SQL query to select the latest update. Also, I chose to run this script from a non-accessible area on my website (outside the public folder) with a cron job every fifteen minutes, keeping my status fairly up to date.

The end result is pretty nice - I simply need to update my Twitter to keep Facebook and my site up to date. Also, it's nice to have the status updates in a local table that I can always play with at a later date.