A php twitter feed

I just made a new site – over at allaboutchris.co.uk (probably plugged that enough now), and I wanted to put on a personal twitter feed. But I didn’t fancy using the javascript plug in straight from twitter, for three reasons.

  1. I don’t like javascript.
  2. The javascript one has an annoying second or two pause whilst it searches for the feed, then suddenly appears, whereas my php has caching.
  3. The javascript one is invisible to search engines.

So, I got busy, and worked out how to do it. Then MaFt wanted to copy my code, so I thought, let’s help the whole world! The tutorial begins below:

This is all possible due to the joys of SimplePie a sexy little php number for parsing and caching rss feeds. Download it here.

So, to incorporate the twitter rss feed, first visit your twitter page, scroll down and click the “rss feed of “name”‘s tweets” link. Copy the address bar, which should read something like “http://twitter.com/statuses/user_timeline/18453541.rss”. That’s your personal RSS feed. Exciting, eh?

Now, at the start of your index.php page, put the following code:

$twits = new SimplePie('http://punkonacross.blogspot.com/rss.xml');

First line includes the simplepie gubbins.
Second line turns the feed into a variable
Third line is what we are going to write now.

Go create twitter.php, and put the following text in:

//change the 0 and 3 to select tweets. 
//This is the first three. If you wanted the second three, choose (3,6)
foreach ($twits->get_items(0,3) as $blogitem):

$blogcopy = $blogitem->get_description();
$blogdate = $blogitem
$bloglink = $blogitem->get_link();

//sort out description

//lose the "bigonroad: " at the beginning - change to whatever your twitter username is
$lookfor = '/bigonroad: /';
$putback = '';
$blogcopy = preg_replace($lookfor, $putback, $blogcopy);

//make links links
$search = array('|(http://[^ ]+)|', '/(^|[^a-z0-9_])@([a-z0-9_]+)/i');
$replace = array('$1', '$1@$2');
$blogcopy = preg_replace($search, $replace, $blogcopy);

//make hashes links
$blogcopy = preg_replace('/(^|[^a-z0-9_])#([a-z0-9_]+)/i', '$1#$2', $blogcopy);

//sort out date
$timenow = date('U');

//change blogdate to text
$difference = ($timenow - $blogdate);
if ( $difference < 1200) {$blogdate = "a few minutes ago";}
if ( ($difference < 2700) && ($difference >= 1200) ) {$blogdate = "about half an hour ago";}
if ( ($difference < 5400) && ($difference
>= 2700)) {$blogdate = "an hourish ago";}
if ( ($difference < 86400)
&& ($difference >= 5400)) {$blogdate = "a few hours ago";}
if ( ($difference < 172800)
&& ($difference >= 86400)) {$blogdate = "some time yesterday";}
if ( ($difference < 604800)
&& ($difference >= 172800)) {$blogdate = "days ago";}
if ( ($difference >= 604800)) {$blogdate = "ages ago";}

//print blog
echo '
echo ''.$blogdate.'';


Not gonna explain it all – you have a brain. But basically, it gets the feed, works out what all the bits are, and adds s to the links – only “@link”s are automatically links in the rss feed, it doesn’t change “http://link.com”s and “#link”s for some reason.

Then it turns 08:10:20.10 08/10/09 into “a few minutes ago”. Feel free to delete that if you would rather have standard dates, or use other settings from the php date() function. If you want to add more stuff like “about 11 days ago”, just work out how many seconds 11 days is (950400), and put that into the equations.

And there you go, voila! See it in action here (okay, just one more plug).

Any questions, just comment!

2 thoughts on “A php twitter feed

  1. in this example:

    foreach ($twits->get_items(0,9) as $blogitem):

    should be

    foreach ($feed->get_items(0,9) as $blogitem):

    cos it needs to match your first bit of code 😉

  2. Just for that I changed the first bit of code instead. Also changed it adds the # links – it does do the @ automatically, I'm fairly sure!

Leave a Reply

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