How to Create an RSS Feed Reader

phplogo.jpg

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.

An RSS feed may look something like this:

rssExample.png

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:

STEP 2

We provide the URL of the RSS feed:

STEP 3

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

STEP 4

We now check if the feed is not empty:

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:

STEP 6

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

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.

STEP 8

We can now convert the title into a link:

I hope you found this tutorial useful.

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