Display your latest Tweet in PHP

So on my mobile site (http://cyte.co.uk/mobile) I created a button to link to my Twitter and this very blog you are reading. This got me thinking what I could do to keep the content fresh without much effort. As the mobile site only consists of a couple of lines of text anyway, I thought a “Latest Tweet” section would be good – because it would be ever-changing. After a bit of googling I tweaked a few bits of PHP I found to come up with the following:

<?php
$username = "cytedesign";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";

function parse_feed($feed) {
    $stepOne = explode("", $feed);
    $stepTwo = explode("", $stepOne[1]);
    $tweet = $stepTwo[0];
	$tweet = htmlspecialchars_decode($tweet,ENT_QUOTES);
    return $tweet;
}

$twitterFeed = file_get_contents($feed);
echo('"'.parse_feed($twitterFeed).'"');
?>

I was so pleased with this in fact that I will use it on the full version of the site as well, and file it away for my employers’ website when we start company Tweeting.

Posted in PHP Tagged with: ,
0 comments on “Display your latest Tweet in PHP
1 Pings/Trackbacks for "Display your latest Tweet in PHP"
  1. […] in 2011 I posted a method for displaying your latest tweet in php. However times change, and the twitter API has been changed beyond all recognition. I have updated […]

Leave a Reply

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

*