How to read RSS feeds using PHP

In this tutorial, I will show how you can use PHP to read RSS feeds of the websites and showing recent post lists using it.

RSS (Rich Site Summary) is a format which is used in many websites which allow web publisher to syndicates their latest posts or data automatically. It also benefits people who want to receive latest posts updates from their favorite websites.

There is another method which allows the user to stay updated is bookmarking. But they need to manually go to websites on the timely basis and check what new have been added.

How to read RSS feeds using PHP


Contents

  1. HTML and PHP
  2. CSS
  3. Conclusion

 


1. HTML and PHP

Using simplexml_load_file() method to read XML which takes URL as a parameter and return an Object after interpreting the XML.

Loop over the Object to get content.

Completed Code

<div class="content">

 <form method="post" action="">
  <input type="text" name="feedurl" placeholder="Enter website feed URL">&nbsp;<input type="submit" value="Submit" name="submit">
 </form>
 <?php

 $url = "https://makitweb.com/feed/";
 if(isset($_POST['submit'])){
   if($_POST['feedurl'] != ''){
     $url = $_POST['feedurl'];
   }
 }

 $invalidurl = false;
 if(@simplexml_load_file($url)){
  $feeds = simplexml_load_file($url);
 }else{
  $invalidurl = true;
  echo "<h2>Invalid RSS feed URL.</h2>";
 }


 $i=0;
 if(!empty($feeds)){

  $site = $feeds->channel->title;
  $sitelink = $feeds->channel->link;

  echo "<h1>".$site."</h1>";
  foreach ($feeds->channel->item as $item) {

   $title = $item->title;
   $link = $item->link;
   $description = $item->description;
   $postDate = $item->pubDate;
   $pubDate = date('D, d M Y',strtotime($postDate));


   if($i>=5) break;
  ?>
   <div class="post">
     <div class="post-head">
       <h2><a class="feed_title" href="<?php echo $link; ?>"><?php echo $title; ?></a></h2>
       <span><?php echo $pubDate; ?></span>
     </div>
     <div class="post-content">
       <?php echo implode(' ', array_slice(explode(' ', $description), 0, 20)) . "..."; ?> <a href="<?php echo $link; ?>">Read more</a>
     </div>
   </div>

   <?php
    $i++;
   }
 }else{
   if(!$invalidurl){
     echo "<h2>No item found</h2>";
   }
 }
 ?>
</div>

2. CSS

.content{
    width: 60%;
    margin: 0 auto;
}

input[type=text]{
    padding: 5px 10px;
    width: 60%;
    letter-spacing: 1px;
}

input[type=submit]{
    padding: 5px 15px;
    letter-spacing: 1px;
    border: 0;
    background: gold;
    color: white;
    font-weight: bold;
    font-size: 17px;
}

h1{
    border-bottom: 1px solid gray;
}

h2{
    color: black;
}
h2 a{
    color: black;
    text-decoration: none;
}

.post{
    border: 1px solid gray;
    padding: 5px;
    border-radius: 3px;
    margin-top: 15px;
}

.post-head span{
    font-size: 14px;
    color: gray;
    letter-spacing: 1px;
}

.post-content{
    font-size: 18px;
    color: black;
}

3. Conclusion

Use simplexml_load_file() method to read RSS feeds of the website. Within the method pass your feed URL.

Loop through the returned Object to read contents.

If you found this tutorial helpful then don't forget to share.

Related Post

Spread the love
  • 18
  •  
  •  
  •  
  •  
  •  

2 Comments

  1. Hello yogesh. i have run you script but i m getting internal server error for that script. please help me.

    May 29, 2018
    Reply
    • Yogesh Singh said:

      Hi Dhiraj,
      I am not viewing any internal server error on your attached link.

      May 29, 2018
      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *