Thirteenth Parallel /archive/rss/

PHP RSS 1.0 Parser

By Daniel Pupius, June 2002

13th RSS 1.0 to anything (13r2a) version 1.0

The RDF tutorial (read it first!) covers something called RDF Site Summary (RSS) which is used for syndication, news feeds and sharing content.  This component is a PHP script which parses a RSS 1.0 XML file using templates, thus you could output HTML, WML, or even JavaScript.  The PHP script can be downloaded and run locally or you can link to it from here (as long as you provide a link back) specifying the XML file and the templates you want to use.

A lot of the news feeds out on the web are still using RSS 0.91, they are not compatible with this script.

This page shows you how to use the PHP script.

Making templates

The parser uses two template files that are specified in the query string, "maintemplate", which wraps the feed and displays information about the channel, and then "itemtemplate" which is used to format each of the individual items in the news feed.

To use the RSS variables in the template we use custom tags in the format:

<?13r2a tagname ?>

The tags that can appear in the main template are:
 - feedTitle - main feed title specified in the "channel" tag
 - feedLink - main link
 - feedDescription - description of the feed
 - imageURL - location of a thumbnail image for the feed
 - imageTitle - title to go with the image
 - imageDescription - (haven't really thought of a use for this one)
 - imageLink - link for image (normally used if you click image)
 - theFeed - where all the items are to go (from other template)
 - numItems - how many items are the RSS file
 - xmlFile - the raw XML file being used

The tags that can be used by the item template are:
 - itemTitle - title of item
 - itemDescription - the main description text for item
 - itemLink - corresponding link
 - itemNo - item's position in list (starts at 1)

So, in the main template you might have the line:

<h1><a href="<?13r2a feedLink ?>">
<?13r2a feedTitle ?></a></h1>

Note that the tags are case sensitive.  More detailed template examples can be seen below.

[Future versions of this script will allow for custom tags or those from another RDF vocabulary, such as The Dublin Core, and also more complex template tags with attributes and logic.]

The script can cope with remote files for both the templates and the XML/RSS files.  This means that you don't actually need to be able to run the PHP script to use it.  The paths that are specified in the query string are relative to the PHP script.  So, if you are running the script on your own server then "/templates/mymaintemplate.htm" is fine, however if you are using the script remotely (i.e. by linking to the script running on 13thparallel) then you need to specify the full URL in the query string, e.g. "http://www.mydomain.com/rss/templates/mymaintemplate.htm".

Example 1: stand alone

This example uses the two templates and the PHP script to create a standalone page.  The main downside is that you will need to specify the full URL and query string and the 13r2a tags need to be placed in the main HTML file.

See it in action

View: main template, item template

Example 2: included page

Here we use minimal template files and then a php include() to paste the parsed file into another document.  This is generally more convenient method than creating a stand alone page.  You could similarly use an SSI or an ASP include to reference the page.

See it in action

View: main template, item template, includes.php

Note the way the templates are specified in the query string.

Example 3: JavaScript

With this example we create the templates in such a way that the PHP script outputs a JavaScript file with the parsed RSS inside.  The way I've done it here is to create an object that holds all the RSS fields, including an array of objects for the items.  To display the RSS file we simply document.write() the variables and loop through the array.  While document.write() isn't the nicest way to insert content into a document using JavaScript, it is easy and will work in version 4 browsers.

See it in action

View: main template, item template

This is the most flexible method if you can't run PHP scripts yourself.  In the <script> tag's src attribute you can reference the PHP script running on our server.  In the query string simply specify full paths to the XML file you want to display and your template files.  e.g:

<script type="text/javascript"
src="http://13thparallel.org/metadata/13r2a.php?
maintemplate=http://yourdomain.com/
templates/javascript.main.template&
itemtemplate=http://yourdomain.com/
templates/javascript.item.template&
url=http://http://yourdomain.com/
mynews.rss"></script>

The example shown here simply displays the RSS fields, however with a bit of imagination there are some pretty dynamic ways you could use this facility.

Conclusion

Well, this is only version 1.0 of the script so bare with me, I'm sure there are a few bugs that need fixing and features that would be useful.  If it turns out people are actually using this script then I'll continue development and make it better.  There are also plans for an ASP version.

Syndication and news feeds aren't the only use for RSS.  It can be a useful, standards-compliant tool for managing your own information.  For example, an RSS file would be the perfect way to store descriptions of your photos.