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.
Displaying the date
The 2010 version adds an argument to display the date:
RSS_Display(url, nombre, true, true);
If the last parameter is omitted, the date is not displayed.
Displaying the channel
The 2009 version allows to display or not the title and the description of the site. It is the third argument of RSS_Display.
By default, it is not displayed. To display it, use this syntax:
RSS_Display(url, number, true);
Otherwise, call:
RSS_Display(url, number);
or:
RSS_Display(url);
for the default number of items, that is 15.
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.
- Displaying an RSS feed on
the same page. - Displaying an RSS feed into
another page. - Displaying only the titles,
on the same page. - Displaying directly a feed
on a page, without to enter the URL.
A demo stylesheet is used too here.

July 23rd, 2010
narayanvj
Posted in
Tags: 











































