Displaying an RSS Feed

How to display an RSS feed with a single PHP function.
The URL of the file may be local, in the form: rss.xml, or distant in the form: http://www.xul.fr/rss.xml.

There is only one difference, if the filename has the ".php" extension when it is generated by a CMS or such software, locally the file is processed by the server as a text file while remotely it is processed as a script. There is no difference if the extension is ".xml".

The script is compatible with:
- RSS 2.0 (that is compatible with 0.91, 0.92 etc.)
- RSS RDF or RSS 1.0.
Actually the script extracts the title, link and description tags and ignores the format of structure of the document.
The benefit of PHP to display an RSS feed is that it will be visible by search engines.

Structure of an RSS 2.0 file

A feed is made of a channel, and one or more items correspondint to articles. Each element has a title, a URL and a description. More details in the specification linked below.

<rss version="2.0">
  <channel>
    <title>Scriptol RSS</title>
    <link>http://www.scriptol.com/rss/</link>
    <description>
        Tools and documents for RSS.
    </description>
    <item>
        <title>The RSS reader</title>
        <link>http://www.scriptol.com/rss/rss-reader.php</link>
        <description>
           Function for displaying an RSS feed.
        </description>
    </item>
  </channel>
</rss>

The interface

The interface is comprised of two functions:

RSS_Display()

For displaying a complete feed with the channel, the titles linking to articles and their descriptions.

RSS_Links()

For displaying only a list of titles that link to articles.

Source code

The source has more internal functions:

RSS_Retrieve(url)            // extract the channel and call RSS_Channel.
RSS_Channel(channel)    // extract data for the channel and call RSS_Tags for each item.
RSS_RetrieveLink(url)     // extract items for a channel and call RSS_Tags for each one.
RSS_Tags(item)            // extract title, link, description for an article.
View the rsslib.php script.

Demos

Demos are templates you can study and use on you own site, according to the Mozilla licence. A form allows to enter the URL of the feed. You can remove the form and replace it by the URL of a feed. The RSS feed may be displayed on the same page or another one.

Download

The archive holds the script and the demos.

Documentation


(c) 2007 Denis Sureau. Scriptol.com
Licence: Mozilla 1.1.