Create an RSS Feed Reader in PHP

An RSS feed reader, also known as an RSS aggregator is a tool used to stay updated on content from different platforms such as websites, blogs, news sites and podcasts. Learn in this post how to create an RSS reader in PHP in simple steps.

RSS Feed Reader in PHP

RSS stands for Really Simple Syndication, sometimes (Rich Site Summary). It is an XML based web format used to distribute content and receive updates from websites. An RSS feed reader gathers updates from different websites that offer an RSS feed in a readable format and displays them like a personalized dashboard. Users using RSS readers do not need to visit websites that interest them for updates. An RSS aggregator is a tool shows updates from multiple websites in one place. In this post, we are going to learn how to create a PHP RSS feed reader in simple steps.

 

How to Create an RSS Feed Reader in PHP?

This is going to be a simple script to demonstrate how to create a PHP RSS aggregator and display updates from certain websites. For demo purposes, I have added a static URL to the RSS feed. What we basically need is just a URL to an RSS feed with a valid RSS format. Steps to create an RSS feed reader in PHP are as follows:

  • Define a variable $rss_feed_url and assign the URL to the feed.
  • Load the content of the RSS feed from the URL using the simplexml_load_file() function, providing the feed URL as a parameter to this function.
  • Check if the RSS was successfully loaded.
  • Display the channel title and description of the RSS feed.
  • Loop through each channel item and display the title of the item in an anchor element with a URL to the channel item.
  • Display the publish date of the channel item in the loop if needed.
  • Display the description of the channel item if needed. I have used the strip_tags() function with allowed tags. You can modify it as per your requirements.

index.php

<!DOCTYPE html>
<html>
<head>
<title>RSS Feed Reader in PHP - Demo</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport"/>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<section class="section py-4">
<div class="container">
<?php
// Define the RSS feed URL
$rss_feed_url = 'https://www.codestacked.info/feeds/posts/default?alt=rss';

// Load the RSS feed content using SimpleXML
$rss = simplexml_load_file($rss_feed_url);

if ($rss !== false) {
?>
<div class="rss-channel">
<div class="border bg-light p-4 mb-4">
<h1><?= htmlspecialchars($rss->channel->title); ?></h1>
<p><?= htmlspecialchars($rss->channel->description); ?></p>
</div>

<ul class="list-none pl-0">
<?php foreach ($rss->channel->item as $item) { ?>
<li class="border bg-white mb-4 p-4">
<h2>
<a href="<?= htmlspecialchars($item->link); ?>" rel="nofollow">
<?= htmlspecialchars($item->title); ?>
</a>
</h2>
<div>
<date>
<small><?= date('d-m-Y h:i:s A', strtotime(htmlspecialchars($item->pubDate))); ?></small>
</date>
<div>
<?= strip_tags($item->description, '<div><p><a><img><ul><ol><li><b>'); ?>
</div>
</div>
</li>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
</section>
</body>
</html>
 

Add RSS CSS Styles

We add some CSS styles for the entire HTML page and RSS feed items.

style.css

* {
box-sizing: border-box;
}
html,body {
margin: 0;
}
body {
background-color: #f6f6f6;
font-family: "Segoe UI", "Roboto", "Helvetica", sans-serif;
font-size: 15px;
font-weight: normal;
font-style: normal;
}
.container {
max-width: 1140px;
width: 100%;
margin-right: auto;
margin-left: auto;
padding-right: 15px;
padding-left: 15px;
}
a {
text-decoration: none;
color: #3778cd;
}
h1, h2{
margin-top: 0;
margin-bottom: 1rem;
font-weight: 600;
}
.list-none {
list-style-type: none;
}
.px-0, .pl-0 {
padding-left: 0;
}
.py-4 {
padding-top: 1rem;
padding-bottom: 1rem;
}
.p-4 {
padding: 1rem;
}
.my-4, .mb-4 {
margin-bottom: 1rem;
}
.border {
border: 1px solid #d1d5db;
}
.bg-light {
background-color: #d1d5db;
}
.bg-white {
background-color: #ffffff;
}

The code snippets in this post can be used as an RSS feed reader in PHP for a single RSS feed. Following these simple steps, we can display multiple RSS feeds on one page or present each URL to multiple RSS feeds as a link and show the complete feed on a separate page.