Displaying RSS Feed with PHP
Wall Script
Follow Me:
Thursday, February 12, 2009

Displaying RSS Feed with PHP

This article explains to displaying RSS(XML format) feed like popurls.com (popular urls in one place) using simplexml_load_file() a PHP function. It's very useful to display your blog feeds as like Recent articles(headlines) list.

RSS- Really Simple Syndication.

Reading XML data and presenting with HTML.

Download Script     Live Demo

Index.php RSS display page:
File contains HTML tags and PHP included rssclass.php. You have to change the RSS feed URL.
<div>
  <?php
  include('rssclass.php');
  $feedlist = new rss('http://feeds2.feedburner.com/9lesson');
  echo $feedlist->display(9,"9lessons");
 
  $feedlist = new rss('http://feeds.feedburner.com/nettuts');
  echo $feedlist->display(9,"Nettuts");
 
  $feedlist = new rss('http://feeds.labnol.org/labnol');
  echo $feedlist->display(9,"Labnol");
  ?> 
  </div>
Popurls.com screen shot

rssclass.php
A beautiful PHP function simplexml_load_file() to load and read XML file. simplexml_load_string() XML string reader.
<?php
 class rss {
     var $feed;

  function rss($feed) 
    {   $this->feed = $feed;  }
 
  function parse() 
    {
    $rss = simplexml_load_file($this->feed);
    
    $rss_split = array();
    foreach ($rss->channel->item as $item) {
    $title = (string) $item->title; // Title
    $link   = (string) $item->link; // Url Link
    $description = (string) $item->description; //Description
    $rss_split[] = '<div>
        <a href="'.$link.'" target="_blank" title="" >
            '.$title.' 
        </a>
   <hr>
          </div>
';
    }
    return $rss_split;
  }
  function display($numrows,$head) 
  {
    $rss_split = $this->parse();

    $i = 0;
    $rss_data = '<div class="vas">
           <div class="title-head">
         '.$head.'
           </div>
         <div class="feeds-links">';
    while ( $i < $numrows ) 
   {
      $rss_data .= $rss_split[$i];
      $i++;
    }
    $trim = str_replace('', '',$this->feed);
    $user = str_replace('&lang=en-us&format=rss_200','',$trim);
    $rss_data.='</div></div>';
    return $rss_data;
  }
}
?>

CSS code :
Style just view the Live Demo
.vas{
    float:left;
    width:270px;
    padding:10px;
}
.title-head {
    font-size:18px;
    font-weight:bold;
    text-align:left;
    background-color:#006699;
    color:#FFFFFF;
    padding:5px;}
.feeds-links {
    text-align:left;
    padding:5px;
    border:1px solid #dedede;
 }
Download Script     Live Demo
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


34 comments:

  1. Thanks man. I'm looking for something like this..

    ReplyDelete
  2. thanks, this script is very usefull :)

    ReplyDelete
  3. hello my friend

    your script is "VeryMuchBonito"
    BUT
    necesito agregar Item->description

    I modify display()
    and OK

    ReplyDelete
  4. hello and I say great resource; is it possible to parse Flickr rss with this class? Thank You for answer me!

    ReplyDelete
  5. Thanks , I recenlty moved from Joomla & Wordpress to jquery powered HTML pages, and I was looking for a script to display RSS XML Feeds in a Web Page Using PHP Functions.

    I hope search engines crawl these links.

    Following you on twitter now
    Masoom : @imasoom

    ReplyDelete
  6. Hello, the script only shows the Title of the rss feed. Is there any way we can display the content of the posts? I am trying to embed all my blog posts, in a webpage, so it looks and feels like a part of the site.

    Masoom

    ReplyDelete
  7. Hello, is it possible to fetch image also with title and link.

    ReplyDelete
  8. How can I get the MEDIA:THUMBNAIL-URL from the item using this codes format?

    ReplyDelete
  9. very good simple script....how do limit description length....I would like to keep under 100.

    ReplyDelete
  10. but when rss feeds of many websites were to display all together, then the webpage loading time increases and it loads very slow.

    ReplyDelete
  11. how can I fetch Images with this code

    ReplyDelete
  12. Thank you so much! This is so easy to implement and style...appreciate your generosity. :)

    ReplyDelete
  13. May i ask you something? I think the "description" item from xml Schema is useless. I mean the below line of your code.The description of every article never appears at any div. Am i wrong?

    $description = (string) $item->description; //Description

    ReplyDelete
  14. how can i get feed url of any sides

    ReplyDelete
  15. Very good script!
    How can I add the posting date before the singles feeds?
    and if I wanted to show all the feed of different sites in a row in a single column?..

    ReplyDelete
  16. Your blog was very insightful. I really enjoyed reading your blog site. I have a blog site as well where I wrote about technical matters related to Computer languages . Do check it out and let me know what you think when u have time .

    ReplyDelete
  17. Your blog was very insightful. I really enjoyed reading your blog site. I have a blog site as well where I wrote about technical matters related to Computer languages . Do check it out and let me know what you think when u have time . http://knowledgeheights.in/

    ReplyDelete
  18. I tried the script out on my server and it does not work like the live demo. Is there anything I may have done wrong?

    ReplyDelete
  19. Please help me.. This is script is showing on my localhost. But not on my server. Is there any way to run this script.. Please help

    ReplyDelete
  20. Excellent thanks! I have a question though, is it possible to have the different feeds going through one table. What I mean is, in the example you have 3 different feeds, each displaying on it's own table - instead of each one having it's own table is it possible to just merge them and sort them by date?

    ReplyDelete
  21. Thanks alot helping me to do it!
    I want to know
    How can i display the summary of the article underneath the article title link?
    Thank you!

    ReplyDelete
  22. Its Fine...I Have one Doubt..How to display Most common news in all feeds..

    ReplyDelete
  23. possible to use paging?

    ReplyDelete
  24. very good article,i used this code in codeigniter framework but does not work, not fetching rss feeds in my local system.
    i made small change in your code simply moved rssclass.php file to libraries folder and called as below in my view
    load->library('rssclass');
    $this->rssclass->rss('http://feeds2.feedburner.com/9lesson');
    echo $this->rssclass->display(9,"9lessons");

    ?>

    ReplyDelete
  25. Excellent entry! I'm been looking for topics as interesting as this.

    ReplyDelete
  26. how we fetch data from https://dfid.blog.gov.uk/feed/atom/ using this script currently its shows noting.

    ReplyDelete
  27. this is a good reference. is it possible for me to display the time and date the posts were made?

    ReplyDelete

Make in India