PHP RSS Reader
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: https://www.scriptol.com/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>https://www.scriptol.com/rss/</link>
<description>
Tools and documents for RSS.
</description>
<item>
<title>The RSS reader</title>
<link>https://www.scriptol.com/rss/rss-reader.php</link>
<description>
Function for displaying an RSS feed.
</description>
</item>
</channel>
</rss>
The functions of the API
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 in the same page.
- Loading an RSS feed into another page.
- Displaying only the titles.
- Displaying a feed in a page by including a PHP function.
A demo stylesheet is used too here. - Displaying a cached version of the HTML code.
Updates
The updates take into account the messages in the forum.
August 2010.
Added a demo for caching the RSS display.
May 4, 2010.
- Added a demonstration of style sheet.
- The rss-get.php demo shows how to add a feed as a parameter to a web page.
- The player also displays the date in option.
Download
The archive holds the script and the demos. The script requires PHP 5.
- RSS Reader. Last version.
Download problems occured with Chrome 5.0.375.99. Try another browser or open a private tab to clear the cache.
Documentation
- RSS tutorial. Description of the RSS 2.0 format and step by step tutorial for building a feed.
- How to put in cached memory an RSS feed. The HTML image is stored on the site.
- How to read a feed in any format. Tutorial.
- RSS 2.01 specification. The standard.
- Using cURL with the RSS reader.
Licence: Mozilla 1.1.
How I can limit the news displayed in RSSLIB ?
kpgmza
scriptol
function RSS_Display($url, $size = 15, $site = 0, $withdate = 0)
The number of links is given to the call to this function:
echo RSS_Display($url, 3);
[ask] having problem when running PHP Reader
atachi
scriptol
$doc = new DOMDocument();
by
$doc = new DOMDocument("1.0");
Delete html display in description
Rik
scriptol
.rss img { display:none; }
Removing data from the description may be achieved by regular expressions, but it is more difficult.rsslib open in new page
JDO2010
scriptol
$page .= "<li><a href=\"$link\">$title</a>";
by:
$page .= "<li><a href=\"$link\" target=\"_blank\">$title</a>";
or any code you choose among _parent, _top, ...RSSLib - which items from a feed - latest or earliest?
happyatom
happyatom
Forum continued...
- Errors and questions about the RSS reader.
- How to start with RSSLib?
- I got a lot of error while reading an RSS feed...
- Charset problem, please help.
- Displaying Danish letters.
- RSS Reader stopped working.
- HTTP/1.0 503 Service Unavailable error with Google News feed.
- How to validate an RSS feed?
- Using RSS lib, URL enclosure.
- Displaying RSS Feed Title.
- DOMDocument Error - Feed No Longer Reads.
- RSSLib - Cannot instantiate non-existent class.
- RSSLib: Nothing happens on my site.
- How to put the RSS Reader into an HTML page?
- How to localize the dates of the RSS feed?
- The reader cannot read this feed.
- How to limit the size of the description?