Create an RSS Feed Reader in PHP

RSS feed reader also known as RSS aggregator is a tool used to stay updated with content from different platforms like websites, blogs, news and podcasts etc. 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 collects updates otherwise feeds from website that offer an RSS in readable format and display them like personalized dashboard. With RSS readers users do not need to visit websites that interest them for updates. RSS aggregator shows updates from multiple websites in one place. In this post we are going to learn how to create PHP RSS feed reader in simple steps.

 

How to Create RSS Feed Reader in PHP

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

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

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 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 RSS feed reader in PHP for 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 link and show complete feed on a separate page.