Here, we have write article about the display RSS feed content with simplePie.
Adding content from a RSS feed on website can be quite a task, thankfully the SimplePie library has helped add the Really Simple back into Really Simple Syndication. In this article we will cover how to add the 5 latest headlines from a RSS feed that link back to the articles from the original website.
Download the SimplePie Library Firstly you will need to download the SimplePie library. Unzip the file and copy simplepie.inc to the root directory of your web server, in this example I am going to add it to the following directory /inc
Reference SimplePie and the RSS Feed at the top of each page. Once you have uploaded the SimplePie library to the server, you will need to reference it at the top of each page the RSS content is going to be displayed on using the following piece of code:
<?php include_once $_SERVER['DOCUMENT_ROOT']. '/inc/simplepie.inc'; ?>
Once the SimplePie library has been loaded you will need to include the location of the RSS Feed or Atom Feed you are wanting to pull the information from by adding the following variable to the block of code above (make sure you replace the example URL with the address of the feed you are wanting to pull the information from).
1 $feed = new SimplePie('http://www.example.co.uk/feed/rss/');
So altogether the block of PHP at the top of each of page should look like this:
1 2 3 4 5 6 <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/simplepie.inc'; $feed = new SimplePie('http://www.example.co.uk/feed/rss/'); ?>
Retrieving the Content Now let's get the RSS content. To do this, we will retrieve an array of posts from the SimplePie object and phrase them into a unodered list using the following code:
1 2 3 4 5 6 7 8 <ul> <?php foreach ($feed->get_items(0, 5) as $item): ?> <li> <a href="<?php print $item->get_permalink(); ?>"> <?php print $item->get_title(); ?></a> </li> <?php endforeach; ?> </ul>
That's everything! You should now have a list of the latest headlines from a RSS feed, linked to the articles from the original website.