Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to display RSS feed using PHP?

In this article we will show how to create dynamic RSS feeds loaded into PHP structures.

The evolution of the Internet has made it possible for us to exercise numerous advantages in plenty of ways. One of such things has to be the availability of news, blogs, audio and video sites that keep us updated with all the latest news around us.

In this regard, you may have likely heard the use of RSS feeds that allows for the availability of data on your favourite applications and even certain websites. RSS has grown to be a very popular means that allows the syndication of web material.

What is RSS?

RSS( Rich Site Summary) is basically a family of web feed formats. This typically allows for the publishing of information that is pretty frequently updated, such as the likes of news articles, blog posts, and audio/video content as well.

Generally, an RSS feed will essentially contain full/summary text, and other metadata that accompanies the text.

How does RSS help?

The use of RSS can be very useful. Since data is constantly or periodically syndicated by means of RSS, one does not need to manually access a website to check for information. Further, users can always have updated content of their favourite websites or channels at all times.

What is an RSS reader?

In order to fully benefit from the features offered by RSS, software that goes by the name of RSS reader, or aggregator, is often used to present such feed data to users. These days there are numerous desktop based RSS readers available,These readers are available for mobile-based applications as well. Popular examples of such readers are Feedly, Digg Reader, Taptu, etc.

Displaying RSS feed by the use of PHP

In order to demonstrate an RSS reader by using PHP, we are going to make use of PHP classes to ensure the inclusion of external feeds.

As you will notice in the code provided below, the topmost parameters basically define the source of the feed. This is followed by the number of items that are to be displayed and where/how long to keep the cached version of the syndicated data.

Succeeding this is the extraction of the data that is relevant, from the array, which is returned by the get RawOutput() function.

We also add some simple forms of HTML formatting to ensure a better presentation of the feed.

Listing 1: A sample PHP code that obtains feed from the NY Times website

  // definition of the parameters of the script
  $BLGURL    = “";
  $NUMITEMS   = 5;
  $TIMEFORMAT = "j F Y, g:ia";
  $CACHFILE  = "/tmp/" . md5($BLGURL);
  $CACHTIME  = 3; // in hours
  // downloading of the feed if a cached version is missing or too old
  if(!file_exists($CACHFILE) || ((time() - filemtime($CACHFILE)) > 4200 * $CACHTIME)) {
    if($feedcontents = http_get_contents($BLGURL)) {
      // writing of feed contents to cache file
      $fp = fopen($CACHFILE, 'w');
      fwrite($fp, $feedcontents);
  include "class.myrssparser.php";
  $rss_parser = new myRSSParser($CACHFILE);
  // reading of feed data from the cache file
  $feeddata = $rss_parser->getRawOutput();
  extract($feeddata['RSS']['CHANNEL'][0], EXTR_PREFIX_ALL, 'rss');
  // display the leading image
  if(isset($rss_IMAGE[0]) && $rss_IMAGE[0]) {
    extract($rss_IMAGE[0], EXTR_PREFIX_ALL, 'img');
    echo "<p><a title=\"{$img_TITLE}\" href=\"{$img_LINK}\"><img src=\"{$img_URL}\" alt=\"\"></a></p>\n";
  // displaying the feed title
  echo "<h4><a title=\"",htmlspecialchars($rss_DESCRIPTION),"\" href=\"{$rss_LINK}\" target=\"_blank\">";
  echo htmlspecialchars($rss_TITLE);
  echo "</a></h4>\n";
  // displaying the feed items
  $count = 0;
  foreach($rss_ITEM as $itemdata) {
    echo "<p><b><a href=\"{$itemdata['LINK']}\" target=\"_blank\">";
    echo htmlspecialchars(stripslashes($itemdata['TITLE']));
    echo "</a></b><br>\n";
    echo htmlspecialchars(stripslashes($itemdata['DESCRIPTION'])),"<br>\n";
    echo "<i>",date($TIMEFORMAT, strtotime($itemdata['PUBDATE'])),"</i></p>\n\n";
    if(++$count >= $NUMITEMS) break;
  // displaying of copyright information
  echo "<p><small>&copy; {",htmlspecialchars($rss_COPYRIGHT),"}</small></p>\n";

If you observe the code comments that have provided to assist you, you will notice that this particular code snippet is responsible for the display of the feed image first, then the title of the feed, followed by the limited number of items from the source as you have set. Be sure to include the copyright code, especially when you are obtaining the content from another source that owns the contents. Many websites do not allow their content to be displayed externally, so make sure that you are familiar with source site’s Terms of Use.

It is also worthy of note that not all RSS feeds need to essentially feature the same format and can differ. So it is up to you to customize your work accordingly.

What we have done above is displayed the first five items from the New York Times’ International Edition page.

Figure 1 – Displaying RSS feed

Analyzing the feed data

When we use different sources for obtaining data feed, including them all can be pretty tricky. This is so because different sites prefer to use different formats of RSS. Further, this will always result in the use of many different formats for data and tag names.

Thus, one of the first steps during analysis would be to examine the raw data that is available and check how all the tags that pertain to it have been nested.

Listing 2: This piece of code is used to fetch data for analysis


The following PHP array will be what the output should look like. From this, you can also see how the code preceding this extracts the data from the source in order to present the image, title, items and copyright information.

Listing 3: Code for the analysis of feed data

    [TITLE] => BBC News - Home
    [LINK] =>
    [DESCRIPTION] => The latest stories from the Home section of the BBC News web site.
    [LANGUAGE] => en-gb
    [LASTBUILDDATE] => Wed, 19 Aug 2015 10:12:42 GMT
    [COPYRIGHT] => Copyright: (C) British Broadcasting Corporation, see for terms and conditions of reuse.
    [IMAGE] => Array
            [0] => Array
                    [URL] =>
                    [TITLE] => BBC News - Home
                    [LINK] =>
                    [WIDTH] => 120
                    [HEIGHT] => 60
    [TTL] => 15
    [ITEM] => Array
            [0] => Array
                    [TITLE] => Bangkok bomber 'is part of network'
                    [DESCRIPTION] => The bomb attack that killed 20 people at Bangkok's Erawan shrine on Monday was carried out by a \"network\", Thailand's chief of police says.
                    [LINK] =>
                    [GUID] =>
                    [PUBDATE] => Wed, 19 Aug 2015 09:27:36 GMT
            [1] => Array
                    [TITLE] => Female libido pill gets US approval
                    [DESCRIPTION] => The US Food and Drug Administration approves a libido-enhancing drug for women that has been dubbed \"female Viagra\".
                    [LINK] =>
                    [GUID] =>
                    [PUBDATE] => Wed, 19 Aug 2015 10:24:02 GMT

Note – The above code is based on feed obtained from the BBC website.

Also, if you find this process to be a little too complicated in nature, you can always go back to the simple HTML code for display that features PHP classes.

Improvements that can be made

One of the drawbacks that you may face with respect to the script defined above is that every $CACHTIME hours, the page loading process will be delayed for any visitor since the entire feed is updated at that time.

However, this can largely be avoided with a CRON job (a server side script that is scheduled) making updates to the $CACHFILE at regular intervals. This means that you do not necessarily have to rely on the PHP script to perform the function.

Also, if this isn’t an available option, you can always resort to modifying the script, in order to allow the fetching process to take place at the page rather than in the middle. And, in order to do so, we take a portion of the code that is responsible for the updating process and modify it to a PHP function.

Listing 4: This code improves the first listed code

function updatFeed()
  global $BLGURL, $CACHFILE;
  if($feedcontents = http_get_contents($BLGURL)) {
    // writing of feed contents to the cache file
    $fp = fopen($CACHFILE, 'w');
    fwrite($fp, $feedcontents);

Following this, the code at the top would also change to

// download the feed when the cached version is missing
  if(!file_exists($CACHFILE)) updatFeed();

It is also worthy of note that this problem can only be an issue when you have a website that attracts a high amount of traffic. If you only get a few visitors to your site, then they will get to view the latest items in the feed only when an update occurs before the display.


Thus, you can largely see from the above-mentioned sample how we have made use of PHP for the display of RSS feeds from some popular websites such as NY Times and the BBC. Generally, there are many ways to obtain RSS feeds, mainly by employing the use of numerous techniques that involve the use of languages as well as their libraries. Often due to the use of different RSS formats by websites, it is required that you analyze the data and then go for customization options for your code accordingly.

RSS readers are very useful applications and allow people to stay updated on content from their favorite news websites and channels at all times.

Website: Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login