Author Biography

Ryan Barr Ryan Barr is a young web standards designer from Colorado. For eight years he has been developing and experimenting with powerful and valid code. Read more about him...

Show Respect

Friendly Partners

  • Scriptplayground
  • (mt) Media Temple

Latest Twitter Update With PHP/RSS - Part Two

Monday, November 3rd, 2008

Taking the script another step with more prefixing, suffixing, and multiple entries.

In part one of this tutorial we have a script that allows us to pull the latest tweet from a Twitter user via the RSS feed that is produced by Twitter. We are also able to set a prefix and a suffix for the tweet for further customization.

In part two we are going to introduce two more prefix and suffix variables along with the ability to pull numerous tweets. With limits set in the Twitter API, we can pull anywhere from 1 to 100. Though it is nice to be able to pull so many, we have to remember that the more we pull, the longer it will take for this script to generate its content.

Before we get to the code, lets go over the exact differences between this script and the script featured in part one. Firstly, we are able to pull more than a single tweet and, secondly, we are able to set a prefix and suffix for the entire block of tweets, along with each individual tweet. With the ability to set these prefixes and suffixes, we can turn our tweets into unordered or ordered lists, div's with alternating background colors, blocks of text, etc. The options are endless and give us more room to work with our tweets than the publicly available methods that Twitter provides.

Lets dive into the code:

<?php

// Your twitter username.
$username = "TwitterUsername";
// Number of tweets to pull in.
$limit = "5";

/* Be sure to escape any quotations in your prefixes and suffixes, such as href=\"link.html\" */

/* These prefixes and suffixes will display before and after the entire block of tweets. */
// Prefix - some text you want displayed before all your tweets.
$prefix = "";
// Suffix - some text you want displayed after all your tweets.
$suffix = "";

/* These prefixes and suffixes will display before and after each tweet in the block. */
// Tweet Prefix - some text you want displayed before each tweet.
$tweetprefix = "";
// Tweet Suffix - some text you want displayed after each tweet.
$tweetsuffix = "";

// It is recommended that you do not modify below this point without PHP knowledge.

$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $limit;

function parse_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) {

$feed = str_replace("&lt;", "<", $feed);
$feed = str_replace("&gt;", ">", $feed);
$clean = explode("<content type=\"html\">", $feed);
$amount = count($clean) - 1;

echo $prefix;

for ($i = 1; $i <= $amount; $i++) {

$cleaner = explode("</content>", $clean[$i]);
echo $tweetprefix;
echo $cleaner[0];
echo $tweetsuffix;

}

echo $suffix;

}

$twitterFeed = file_get_contents($feed);

parse_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix);

?>

There are obviously some major changes in this script from our previous one, such as the introduction of a loop and echoing directly from the function.

When using this code, please be sure you recognize all six variables that can be changed: $username, $limit, $prefix, $suffix, $tweetprefix, and $tweetsuffix. Let's go over what each does:

  • $username - Specifies which user to pull the tweets from.
  • $limit - Specifies how many tweets to pull from the user defined in the $username variable.
  • $prefix - Specifies the text or code to insert before the entire block of tweets.
  • $suffix - Specifies the text or code to insert after the entire block of tweets.
  • $tweetprefix - Specifies the text or code to insert before each tweet.
  • $tweetsuffix - Specifies the text or code to insert after each tweet.

What can this allow us to do? Let's go through two examples:

To achieve a simple tweet box, use the following variable settings:

$prefix = "<strong>John Doe (<a href=\"http://twitter.com/username/\">@TwitterUsername</a>) Tweets:</strong><br />";
$tweetsuffix = "<br />";

Using the above code, you will get a result looking like this:

John Doe (@TwitterUsername) Tweets:
This is a tweet.
This is a tweet.
This is a tweet.
This is a tweet.
This is a tweet.

Or, to achieve a unordered list that can be styled with CSS, try these variable settings:

$prefix = "<ul class=\"tweetbox\"><li><strong>John Doe (<a href=\"http://twitter.com/username/\">@TwitterUsername</a>) Tweets:</strong></li>";
$suffix = "</ul>";
$tweetprefix = "<li>";
$tweetsuffix = "</li>";

Using the above code, you will get a result looking like this:

  • John Doe (@TwitterUsername) Tweets:
  • This is a tweet.
  • This is a tweet.
  • This is a tweet.
  • This is a tweet.
  • This is a tweet.

Obviously with the small amount of code that has been added and changed since part one, we are able to do a lot more with our tweets. Was this a bit too much for you? Try revisiting the first part of the tutorial to get a refresher.