How To Read And Display Rss Feed Using PHP OOP

How To Read And Display Rss Feed Using PHP OOP
how to display rss feed from another website using php,Read rss feed with php, reading rss feed with php, display rss feed with php
Hello our readers in this article you will learn how to display RSS feed with PHP,OOP RSS means (Rich Site Summary) which allow user to read latest article or news from website, without visting each website or webblogs.


First, Check Basic Structure of RSS FEED, So whenever you pull data from the RSS feed you get data in array Like below

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
  <title>Meralesson Home Page</title>
  <link>http://www.meralesson.com</link>
  <description>Free web building tutorials and scripts</description>
  <item>
    <title>RSS Tutorial</title>
    <category>RSS Label</category>
    <pubDate>12/14/2015</pubDate>
    <author>Meralesson</author>
    <link>http://www.meralesson.com/</link>
    <description>New RSS tutorial on W3Schools</description>
  </item>
  <item>
    <title>XML Tutorial</title>
    <category>XML Label</category>
    <pubDate>12/14/2015</pubDate>
    <author>Meralesson</author>
    <link>http://www.meralesson.com/</link>
    <description>New XML tutorial on W3Schools</description>
  </item>
</channel>
</rss>
Copy main class below, In this class I made a function called feed when you pass feed url then it will return feed data, this function made with PHP simplexml_load_file function

<?php 
  class main{
//fetch data from feed url
public function feed($feed_url){
 $feed = simplexml_load_file($feed_url);
 //Set limit for links
 $limit = 8;
 $x = 1;
 //Fetch all data from feed
  foreach ($feed->channel->item as $item) {
          if($x <= $limit){
     //Coverting the feed date time to date
              $date     = date("jS M , Y", strtotime($item->pubDate));
       $title    = $item->title;
       $url  = $item->link;
       $category = $item->category;
        echo '<div class="rss-full">
                <ul>
                    <li>
                        <div class="rss-head">
                            <div class="left-div"><h5>'.$category.'</h5></div>
                            <div class="right-div"><small>'.$date.'</small></div>
                        </div>
                    </li>
                    <li>
                        <div class="rss-body">
                            <a href="'.$url.'">'.$title.'</a>
                        </div>
                    </li>
                </ul>
      </div>';
      }
     
     $x++;
  }
    }
}
?>
Now lets move on index.php copy all below codes

<?php
// Include the main class file 
 include 'core/main.php';
        //Set feed url
 $feed_url = "http://feeds2.feedburner.com/meralesson";
        //Instantiate the class 
  $get = new Main;
?>
<!Doctype html>
<html>
  <head>
     <title>RSS FEED - Meralesson.com</title>
      <link rel="stylesheet" href="css/style.css"/>
  </head>
<body>
   <div class="rss-box">
      <div class="rss-heading">
        <h4>RSS FEEDS From Meralesson.com</h4>
       </div>
         <div class="rss-inner">
            //Call the function by passing feed url
             <?php $get->feed($feed_url);?>
        </div>
      </div>
    </body>
</html>
 
In the end lets make this RSS feed beautiful by adding the css, Copy below css style

/*
 Design by Meezan Ud Din
*/
*{
    margin: 0px;
    padding: 0px;
    width: auto;
    height: auto;
    color:#292F33;
    font-family:Helvetica, Neue;
}
body{
    background-color: #f0f0f0;
}
small,span,b{color:#999999;font-size: 12px;
}

a{color:7195E5;text-decoration:none;}

a:hover,a:active{
    text-decoration:underline;
}   

.rss-box ul {list-style: none;margin: 2px 4px;}
.rss-box ul li{}
.rss-box{
    width: 400px;
    margin: 100px auto;
    background-color: white;
    border-radius: 3px;
}

.rss-heading{
    width: 100%;
    height: 30px;
    border-bottom:1px solid #E8E8E8;
}
.rss-heading h4{
    font-size: 14px;
    line-height: 30px;
    padding: 2px 5px;
}

.rss-inner{
    width: 100%;
    height: 300px;
    overflow: auto;
}

.rss-full{
    border-bottom:1px solid #E8E8E8;
    margin: 10px auto;
}   


.rss-head{
    width: 100%;
    height: auto;
    overflow:auto;
}

.left-div{
    float:left;
    width: 50%;
}
.left-div h4{
    
}

.right-div{
    float:right;
    width: 50%;
    text-align: right;
}

.rss-body{
    width: 100%;
}

.rss-body a{
    font-size: 13px;
    color: dodgerblue;
}

#credits{
 margin: -7% auto;
 }

Comments