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 the latest posts updates from their favorite websites.

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

How to read RSS feeds using PHP


Contents

  1. HTML and PHP
  2. CSS
  3. Demo
  4. Conclusion

1. HTML and PHP

Using simplexml_load_file() method to read XML which takes URL as a parameter and returns 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 "<h2>".$site."</h2>";
  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. Demo

View Demo


4. 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.

23 thoughts on “How to read RSS feeds using PHP”

  1. Just a little problem.
    If content have html tags, like opening a DIV, the list is KO.
    You have to add a strip_tags()

    Like this : $description = strip_tags($item->description);

    Reply
  2. Hello Yogesh,

    I’m fairly new to PHP and I’m trying to make sense of RSS Feeds. How would I go about starting the page without the initial Makeitweb Feed and just the form by itself? I tried moving things around and taking away the initial URL link within $url, but nothing seems to work.

    Thank you.

    Reply
      • I tried adding this and then this line: <img src="”/> in the loop but didn’t work, nothing is showing. IN my feed there are many images, even into the enclosure tags. I don’t get why it doesn’t show

      • @MTT
        You can use <img src="”/> with his example code (above you).

        It worked well for me.

        @Yogesh
        “RSS” actually stands for “Really Simple Syndication”. BTW: Nice article! I did a few mods to the source code, so that I can make it a “drop-in” solution for most websites.

        – Great work!

        – Jim S.

  3. Great code. worked perfectly for my project. Please assist on how i can order the results by date in descending order, ie starting with the latest article on top.

    Reply
      • That’s a good idea.

        Usually, most CMS’s that generate RSS feeds should already sort them chronologically.

        If you sort them before displaying, that gives you control of how they are sorted. Neat!

        BTW: Have solved the problem of getting and displaying associated images (IE: “featured Images”) as some RSS feeds may not have a featured image, so simply getting and displaying the first embedded image was a neat idea.

        – Jim S.

  4. Hi Yogesh

    I am quite new to php. Can you assist me on how i insert values into an array. i believe this should be a multidimensional array?

    Reply
  5. Hello Yogesh thanks for your code, it is very helpfull.
    i have a problem
    the code work perfectly in localhost but when i put it online “invalid rss feed url”.
    Any idea for this error please?

    Reply
    • I have the same problem. Works beautifully on my local WordPress install but the same code won’t work on the live site. I’m thinking its related to the Sucuri firewall.

      Reply
  6. Hi bro, How to do the same for Dynamic PHP Mysql Website i tried many ways but not gets anything

    My website is Custom made Dynamic php mysqli website.. You can Check my site as well bro cricketulagam.com
    How can make Rss Feed for it

    Pls Reply bro

    Reply

Leave a Comment