CODING ACADEMY

View Original

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.

An RSS feed may look something like this:

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:

See this content in the original post

STEP 2

We provide the URL of the RSS feed:

See this content in the original post

STEP 3

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

See this content in the original post

STEP 4

We now check if the feed is not empty:

See this content in the original post

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:

See this content in the original post

STEP 6

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

See this content in the original post

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.

See this content in the original post

STEP 8

We can now convert the title into a link:

See this content in the original post

I hope you found this tutorial useful.

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