Parse a twitter feed with php (part 1)

31Oct10

I thought finding some php code to parse a twitter feed would be fairly straightforward…
…although a search of Google returned code that ranged from elegant to incomprehensible, I still couldn’t find anything that quite fitted the bill. So with the help of php.net I wrote something from scratch that pretty much does what I want.

The code below uses the simplexml_load_file function to turn a twitter RSS feed into an object. From there it strips out some unwanted characters, adds a hyperlink (if one exists in the original post), strips down the date to DD MMM, adds some html and returns the three most recent posts.

There are plenty of notes in the code (probably too many for most people), but down the road I WILL forget what I was doing, so I need them:o)

You will obviously need to change the twitter RSS feed and probably change/strip out some of the html (namely the span tags). Please feel free to use the code, if you’re feeling generous maybe give me a mention:o)

<?php

$twitterRSS = simplexml_load_file ( ‘http://twitter.com/statuses/user_timeline/177618489.rss&#8217; ); //Noel’s twitter

$i = 0;

while ($i <= 4) {

$description = $twitterRSS->channel->item[$i]->description;

$pubDate = $twitterRSS->channel->item[$i]->pubDate;

$guid = $twitterRSS->channel->item[$i]->guid;

//remove username, colon, and single white space…

$colon = ‘:’;

$positionColon = strpos($description, $colon);

$description = substr_replace ($description, ” , 0 , ($positionColon +2) );

//find the position of ‘http://&#8217;

$http = ‘http://&#8217;;

$positionHttp = strpos  ( $description  , $http );

if ($positionHttp == !NULL) //i.e. there is a http:// link, then execute the code

{

//find the first white space after the ‘h’… (if there is one – links are often at the end!)

//we will use strpos again, but this time include the offset parameter to choose where to start our search…

$whiteSpaceAfterHttp = strpos  ( $description , ‘ ‘ , $positionHttp );

//the last character of the link is $whiteSpaceAfterHttp value minus 1

$linkLastChar = ($whiteSpaceAfterHttp – 1);

//now… if there was no white space after the link $linkLastChar will be -1 !

//if this is the case we can make $linkLastChar the string length!

//this will be the position of the last character… haha

if ($linkLastChar < 0) {

$linkLastChar = strlen ( $description );

}

//we now have the beginning and end positions of the http link

// $positionHttp and $linkLastChar

//let’s get the link length…

$linkLength = ($linkLastChar – $positionHttp);

//let’s grab the link that needs ‘fixing’…

$oldLink = substr ( $description , $positionHttp , $linkLength );

//let’s add html tags to the old link and call it $newLink

//$newLink = ‘<a href=”‘ . $oldLink . ‘”>’ . $oldLink . ‘</a>’;

$newLink = ‘<span><a href=”‘ . $oldLink . ‘”>’ . $oldLink . ‘</a></span>’;

//let’s swap the old link for the new link in the string…

$description = substr_replace  ( $description  , $newLink  , $positionHttp , $linkLength );

}

//ok, so the RSS date in a twitter tweet comes out something link this:

//Mon, 04 Oct 2010 11:37:43 +0000

//remove the first 5 characters…

$pubDate = substr_replace  ( $pubDate , ‘ ‘ , 0 , 5 );

//remove the remaining 21 characters from position 7 onwards…

$pubDate = substr_replace  ( $pubDate , ‘ ‘ , 7 , 21 );

//add the link to the date with html…

//$pubDate = ‘<a href=”‘ . $guid . ‘”>’ . $pubDate . ‘</a>’;

$pubDate = ‘<span><a href=”‘ . $guid . ‘”>’ . $pubDate . ‘</a></span>’;

echo ‘<p>’;

echo $description;

echo ‘</p>’;

echo ‘<h4>’;

echo $pubDate;

echo ‘</h4>’;

echo ‘<div class=”hr”>&nbsp;</div>’;

$i++;

}

?>

Advertisements


2 Responses to “Parse a twitter feed with php (part 1)”

  1. 1 Matt

    Thanks for the script! I too was having one hell of a time finding something to work. After a couple of syntax changes, I was able to get something I can work with.

    I added two single quotes instead of a double in this line:
    $description = substr_replace($description, ” , 0 , ($positionColon +2) );

    Also, there were two lines that I replaced the dash in too.

    • 2 noelbyrne

      Hi Matt, thanks for your comment much appreciated:o)

      I found a little bug in the script I wrote which meant only a hyperlink at the end of a tweet would work. If the link was somewhere in the middle then it would be one character short (as it where). I posted some new code here for anyone that’s interested: Parse a twitter feed with PHP.
      Noel


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: