PHP Tutorial – Retrieving Contents From Website Feed via SimplePie


While i was writing my HTTP Header Checker tool, i decided to include latest blog post of this blog at the footer. i wanted a system to automatically retrieve my latest blog post, so i finally settled for SimplePie a fast, easy-to-use, RSS and Atom feed parsing in PHP to do the heavy lifting. all you need do is specify the feed URL(s) and SimplePie will retrieve, display and update your given web page with latest content found in the feed.

SimplePie Installation
  • Head over to SimplePie.org and download the latest updated version.
  • In your web host root folder(public_html) or any installation folder of your choice, create two folders php and cache.
  • From your SimplePie downloaded archive, copy or upload library folder and autoloader.php file to the php folder you just created.
  • At this point, you’ve successfully install SimplePie
Using SimplePie

Example I

In this first example, i am going to display all(10) blog post found in tech4sky.com RSS feed powered by feedburner i.e http://feeds.feedburner.com/tech4sky.

tech4skyfeed

  • We need to configure SimplePie setup. firstly, include and load the autoloader.php in “PHP” folder using either include or require function.
    <?php include_once('php/autoloader.php'); ?>
  • Next is to add the below SimplePie setup. i commented the code to explain what they do.
    <?php 
    // We'll process this feed with all of the default options.
    $feed = new SimplePie();
     
    // Set which feed URL to process.
    $feed->set_feed_url("http://feeds.feedburner.com/tech4sky");
     
    // Run SimplePie.
    $feed->init();
     
    // This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
    $feed->handle_content_type(); 
    ?>

    Note change http://feeds.feedburner.com/tech4sky above to your desire RSS feed url.

  • Finally, we need to retrieve and output the content found on the feed. note, i’m not going to include any CSS markup. you’ll have to do that yourself.
     
    // displaying the feed title with description as the by-line
    <h1><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h1>
    		<p><?php echo $feed->get_description(); ?></p>
    

    to display the feed content, we need to loop through all of the items in the feed, and $item represents the current item in the loop.

     <?php foreach ($feed->get_items() as $item) {?>
    
    // display blog posts title with each iteration
    <h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
    
    //Display post content either full or excerpt
    <p><?php echo $item->get_description(); ?></p>
    			
    // date post was published
    <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
    <?php } ?>
  • Run the code, and you should be able to retrieve latest content in your RSS feed.
  • Demo => Click here

    Download Source code from my Repo. @ => GitHub

Example II

Remember in the example above, the code output all feed contents found, but in this, am going to explain how to limit the number of content or rather blog post to display. to achieve this, we are only going to modify the foreach code.

<?php 
$count = 0; //First we set the counter to be zero

$output = 4; //This is the desired number of content to display

foreach ($feed->get_items() as $item) :
$count++;
?>

// display blog posts title with each iteration
<h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>

//Display post content either full or excerpt
<p><?php echo $item->get_description(); ?></p>
			
// date post was published
<p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
<?php if($count == $output) break; 
 endforeach; ?> 

Example III

Let’s take a look at how to display content from multiple feeds. for this to work, we are going to set the value of $feed->set_feed_url to a PHP array containing the feed URLs to parse. By default, SimplePie will re-order the content by date.
Say we want to display blog post from techethix and bloggingtipstoday.com, the value of $feed->set_feed_url will be;

$feed->set_feed_url(array(
"http://bloggingtipstoday.com/feed",
"http://techethix.blogspot.com/feeds/posts/default",
)); 

Demo => click here
Download source code


About Agbonghama Collins

Agbonghama Collins is a webdeveloper and an internet geek. I love writing and sharing the little idea & information i know. Hope you enjoyed my Article.

Follow me on Twitter, add him on Facebook or on Google+

  • http://onenaijablog.com Babanature

    Hello Collizo,
    This is really a nice tutorial and i must say that you are indeed hot in php :).
    I have never used the simplepie before but the way you demonstrated it here, it looks good. I can see that the code doesn’t affect server that much :).
    Nice tutorial and do keep the good work rolling here…

  • http://techethix.blogspot.com/ Rohit Asare

    Cool, 1 more awesome WordPress Tutorial :)
    and Thanks for adding my blog, man :)
    Your Tech Diary is Awesome ;)