Twitter recently changed their API to support OpenAuth which is by-and-large a good thing.

Unfortunately, however, they stopped supporting basic RSS feeds, which was my preferred method of printing twitter status updates on the web.

Not to fear, as it turns out you can still access an XML feed of any user’s status updates without using OpenAuth, the feed can be accessed at http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=your_twitter_handle.

This makes life much easier! What I’ve done is put together a very basic script that grabs the feed, parses it, makes urls into links, and outputs it as an unordered list (easy enough to change should you choose to do so).

You can call this script asynchronously if you prefer…I didn’t include any code for this but it’s simple and can be with the jQuery ajax() function.

Please note that this script uses SimpleXML, which is only supported in PHP 5+. I packaged this in an extra file (twitter_feed.php) and I call it with an include() or jquery ajax() function.

<?php

/*
 * Parses and outputs a twitter feed
 * Packaged at http://www.johnpezzetti.com
 * URL Link conversion function from DZone http://snippets.dzone.com/posts/show/6156
 */

$twitterUsername = 'pezigner'; //Twitter Username
$numTweets = 10; //Number of entires to load

function build_twitter_feed($username,$numEntries){
  $xml = simplexml_load_file("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=".$username."&count=".$numEntries);
  if ($xml){
    $output = '<ul class="twitter-feed">';
    foreach($xml->status as $tweet){
      $text = make_urls_links($tweet->text);
      $date = $tweet->created_at;
      $output .= '<li>'.$text.'</li>';
    }
    $output .= '</ul>';
  }
  return $output;
}

function make_urls_links($text){
    $ret = ' ' . $text;
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a target=\"_blank\" href=\"\\2\" >\\2</a>'", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a target=\"_blank\" href=\"http://\\2\" >\\2</a>'", $ret);
    $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
    $ret = substr($ret, 1);
    return($ret);
}

echo build_twitter_feed($twitterUsername,$numTweets);

?>

No Responses to “Printing xml twitter status updates without OpenAuth now that RSS Feeds are gone”  

  1. No Comments

Leave a Reply