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.

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.

Documentation

Licence: Mozilla 1.1.

Forum

How I can limit the news displayed in RSSLIB ?

2011-11-23 10:00:21

kpgmza

Hi friends: I'm using RSSLIB and works great! Can you tell me how do I limit the number of news shown?. I wish only showing the last 3 (most recent). Is this possible? Well.. I await for your news.. thank you very much!
2011-11-25 15:18:36

scriptol

Hello, The size of the feed is one of the parameters of the function RSS_Display:
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

2011-06-20 16:50:49

atachi

sorry, i am students.............i have a question every i running this program "localhost/semantik/rsslib/rss-links.php" i have the error message like this Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in D:\xampp\htdocs\semantik\rsslib\rsslib.php on line 95 Fatal error: Call to undefined method domdocument::load() in D:\xampp\htdocs\semantik\rsslib\rsslib.php on line 77 how do i solve this error??? thank you
2011-06-21 03:04:35

scriptol

The problem is in the version of PHP. You can modify the rsslib.php script, replace:
$doc  = new DOMDocument();
by
$doc  = new DOMDocument("1.0");

Delete html display in description

2010-11-23 10:57:08

Rik

Hi, I'm trying to create a rss of google news, but I would like to display only plain text without the html stuff like images. (see rss google site for example news.google.nl/news?q=dengue&hl=nl&um=1&ie=UTF-8&output=rss) How can I adjust this in the rsslib file? Any help is welcome
2010-11-24 03:33:16

scriptol

Hello. The simplest way is to turn the display off in the CSS. Providing the RSS layer has the .rss class name:
.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

2010-09-06 00:24:18

JDO2010

I wanted to know how to open pages from the rss feed in a new page? do I need to put target="_blank" somewhere in the code? Thanks, Jeremy
2010-09-06 02:27:51

scriptol

You can define the way the page is opened. In the function RSS_Display or RSS_links, replace
$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?

2009-06-14 12:05:48

happyatom

Hi Does RSSLib take the latest items in a feed - for instance if I publish items on my blog and set RSSLib to show 3 items from my feed my website, will the feed on my website automatically update to show the 3 latest blog entries? thanks in advance Paul PS and many thanks for making the script freely available - it so much easier to use than everything else I have looked at.
2009-06-14 12:15:22

happyatom

Sorry - easy for me to test this of course - it shows the most recent! thanks again

Forum continued...