Twitter-RSS-to-HTML: Display Twitter Updates with PHP

Twitter-RSS-to-HTML is a PHP script that parses a Twitter user's RSS feed and displays updates as HTML. The script can be used to incorporate one or more Twitter updates on a weblog or another site.

Version 1.1.

Links:

This software was developed by Rogers Cadenhead, publisher of the Workbench weblog, and has been released under the GNU General Public License.

I'm running the software on PHP 4.3.2 and MagpieRSS 0.7. I think it should work with PHP 4 or higher, but that's only a guess.

Installation instructions

If you haven't installed MagpieRSS yet, save its files in a magpie subdirectory of your PHP installation's include_path. For example, /usr/local/lib/php/magpie on a Linux server.

Next, save the Twitter-RSS-to-HTML script in a directory where it will be accessible to PHP web pages. Make sure the file has the extension .php.

Documentation

Twitter-RSS-to-HTML employs the following variables:

The script turns URLs in Twitter messages into hyperlinks and turns "@" references into a link to the user's Twitter page.

Updates in a Twitter RSS feed begin with the sender's username followed by a colon. The $username variable is employed to filter this out and just display the text of the update.

The $page variable filters out Twitter messages that link to the site where this script will be used. If I link to an entry from my weblog in a Twitter message, I don't want that message displayed on my blog. Set $page equal to the empty string "" to turn off this functionality.

The MagpieRSS RSS library for PHP caches feeds for an hour and will use cached data whenever a feed can't be accessed. So during the times when Twitter can't be accessed, you can use this script without hurting your site's performance. I've been running it for a month and have not experienced any problems when Twitter was offline or slow.

After you have set up the script, you can include it in another web page published with PHP by using the following code:

<div id="twitter_div">
END;

include('twitter-rss-to-html.php');

ECHO <<<END
</div>

The script wraps each Twitter update in a tweetwhen class so that you can use Cascading Style Sheets (CSS) to change its presentation. In the preceding example, the twitter_div class wraps all updates for further customization.

  • Version 1.1: Added underscores ("_") to the valid username characters in an "@" link, July 10, 2008
  • Version 1.0: Original release, July 8, 2008
License

Copyright 2008 Rogers Cadenhead

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.