Putting Twitter on Your Blog

Today I wanted to put my Twitter feed on my blog but didn’t really want to use a widget since that is for distribution purposes and I was looking for something more permanent. Twitter gives everyone the ability to see their tweets via rss or atom feeds so I choose to use the rss feed. Mine feed was located at http://twitter.com/statuses/user_timeline/2376811.rss. If you have PHP5 then parsing out an XML (rss) file is very easy thanks to the SimpleXML functions. One small problem that I ran into was that my host doesn’t allow me to read in the file like I wanted to. I had to use CURL to read the file save it locally and then reopen that file to parse it with SimpleXML. Let step though the code. First you have to get the data from the rss feed and use curl to store this as a new file on your local server.

//open the remote fee from twitter.
$ch = curl_init("http://twitter.com/statuses/user_timeline/2376811.rss");
//overwrite this feed to a local file
$fp = fopen ("twitter_feed.xml", "w");
curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);

Then you have to parse the local file that you just created. Twitter’s RSS feed puts the feed data in item nodes but nested in a channel node. Here we will use SimpleXML (PHP5 Only!) and a foreach loop to parse through each item node.

$xml = simplexml_load_file ("twitter_feed.xml");
foreach ($xml->channel->item as $item) {
echo str_replace ("farrelley:", "", $item->description);
}

Basically that is the heart of the code. Next we will put it all together into a function. We will also add in a counter that when you call the function you can specify how many item nodes or tweets you want to see.

// Display Twitter Feed
function getTwitterFeed($disp) {
//use Curl to get the feed and store it locally
$ch = curl_init ("http://twitter.com/statuses/user_timeline/2376811.rss");
//open the local feed.
$fp = fopen ("twitter_feed.xml", "w");
//set the counter = 1 its the first time through the script/
$count = 1;
curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);
//parse through locally stored feed
$xml = simplexml_load_file ("twitter_feed.xml");
foreach ($xml->channel->item as $item) {
//strip out my username each description has username: at beginning.
echo str_replace ("farrelley:", "", $item->description);
//if the count = the number to display then break out of foreach
if ($count == $disp) break;
//else increment count and continue.
else $count++;
}
}

Then you just have to add this call to the function where you want the data to be output.

getTwitterFeed (1);

You can see this in action (on the right sidebar) on my other blog at Living in the District (http://www.livinginthedistrict.com/). Like always and comments please post comments.

One thought on “Putting Twitter on Your Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>