How to Create an RSS Feed Reader

In this lesson we will take an external RSS feed, read it using the PHP simplexml_load_file function and then loop through the file presenting the feed on our website.

For this example I will use the RSS feed available on this website which can be found at http://coding.academy/rss.

The RSS feed looks like this:

RSS feed example

Note how the RSS feed is structured.  We have the main channel and then, after the main title, link and description, we have the items. 

Each item in the RSS feed has:

  • title
  • link
  • description

STEP 1

The first thing we will do is create a basic HTML5 template as follows:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>RSS Reader</title>
   </head>
   <body>
     
   </body>
</html>

STEP 2

We provide the URL of the RSS feed:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>RSS Reader</title>
   </head>
   <body>
      <?php
         $rssUrl = "http://coding.academy/rss/";
      ?>
   </body>
</html>

STEP 3

We now grab the RSS feed and assign it to a variable:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>RSS Reader</title>
   </head>
   <body>
      <?php
         $rssUrl = "http://coding.academy/rss/";
         $rssFeed = simplexml_load_file($rssUrl);
      ?>
   </body>
</html>

STEP 4

We now check if the feed is not empty:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>RSS Reader</title>
   </head>
   <body>
      <?php
         $rssUrl = "http://coding.academy/rss/";
         $rssFeed = simplexml_load_file($rssUrl);
         if(!empty($rssFeed)){
            
         }
      ?>
   </body>
</html>

STEP 5

Now let's handle the actual retrieval of each of the items in the feed. We need to loop through the RSS feed and grab each item. We can do this by using a foreach loop:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>RSS Reader</title>
   </head>
   <body>
      <?php
         $rssUrl = "http://coding.academy/rss/";
         $rssFeed = simplexml_load_file($rssUrl);
         if(!empty($rssFeed)){
            foreach($rssFeed->channel->item as $item){
               
            }
         } 
      ?>
   </body>
</html>

STEP 6

Within this loop we can grab the title, link, and description of each item:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>RSS Reader</title>
   </head>
   <body>
      <?php
         $rssUrl = "http://coding.academy/rss/";
         $rssFeed = simplexml_load_file($rssUrl);
         if(!empty($rssFeed)){
            foreach($rssFeed->channel->item as $item){
               $title = $item->title;
               $link = $item->link;
               $description = $item->description;
            }
         } 
      ?>
   </body>
</html>

STEP 7

Now that we have the required data we can output it in any form we wish. In this case I will keep things simple and just use div elements.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>RSS Reader</title>
   </head>
   <body>
      <?php
         $rssUrl = "http://coding.academy/rss/";
         $rssFeed = simplexml_load_file($rssUrl);
         if(!empty($rssFeed)){
            foreach($rssFeed->channel->item as $item){
               $title = $item->title;
               $link = $item->link;
               $description = $item->description;
               echo "<div>" . $title . "</div>";
               echo "<div>" . $description . "</div>";
            }
         } 
      ?>
   </body>
</html>

STEP 8

We can now convert the title into a link:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>RSS Reader</title>
   </head>
   <body>
      <?php
         $rssUrl = "http://coding.academy/rss/";
         $rssFeed = simplexml_load_file($rssUrl);
         if(!empty($rssFeed)){
            foreach($rssFeed->channel->item as $item){
               $title = $item->title;
               $link = $item->link;
               $description = $item->description;
               echo "<div><a href='" . $link . "'>" . $title . "</a></div>";
               echo "<div>" . $description . "</div>";
            }
         } 
      ?>
   </body>
</html>

I hope you found this tutorial useful.

You could extend this code by adding CSS styling and some error checking on the RSS loading.

Want to Learn More?

Become a member and get access to the member only area of this site here.

Comments

Your email address will not be published.

User Avatar

Excellent! Just what I was looking for!

by KingPin, 02-Jan-2016

PHP for Beginners

Enrol in the full course here

PHP, MySQL and PDO

Coming soon, a full course on using PHP, MySQL and PDO. Be notified as soon as the course goes live.