How to read RSS feeds using PHP

In today’s fast-paced world, staying up to date with the latest news and blog posts is essential. One of the easiest ways to do this is by subscribing to RSS feeds, which allow you to receive the latest updates from your favorite websites directly in your RSS reader.

RSS (Really Simple Syndication) is a format that is used in many websites which allow web publisher to syndicate their latest posts or data automatically.

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

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

How to read RSS feeds using PHP


Contents

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

1. HTML and PHP

HTML

Create a <form >. In the <form > create a textbox and a button. The textbox is use to enter the feed URL.


PHP

Assigned default feed URL in $url variable, its value gets updated when <form > is submitted.

Using simplexml_load_file() function check URL is valid or not. If URL is valid then it 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

Reading RSS feeds using PHP is a simple and powerful way to stay up to date with the latest news and blog posts from your favorite websites. By following above mentioned steps in this article, you can create a basic RSS reader that loads and parses an XML file, extracts the information you need, and outputs it in a useful format.

Use simplexml_load_file() function to read RSS feeds of the website. Provide your feed URL into the function, then loop through the returned Object to view the contents.

If data is not loading then check the XML file structure and update the code accordingly.

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