A PHP RSS feed reader reads an RSS XML feed and displays the feed items on a webpage. RSS feeds usually contain a channel, title, link, description, publish date, and a list of items.
Since RSS is XML, we can read it in PHP using SimpleXML. This is useful when you want to show recent blog posts, news updates, product updates, or announcements from an RSS feed.
This tutorial shows how to read an RSS feed in PHP, parse the feed items, and display them in a clean HTML layout. If you want to compare different XML parsing methods, read the main guide on PHP XML Parser. This article focuses only on RSS feed reading as a practical XML example.
Quick Answer
Use simplexml_load_file() to load an RSS feed URL or file. Then loop through $rss->channel->item to read each feed item.
<?php
$rss = simplexml_load_file('sample-feed.xml');
foreach ($rss->channel->item as $item) {
echo $item->title . PHP_EOL;
echo $item->link . PHP_EOL;
}
?>
The simplexml_load_file() function returns a SimpleXMLElement object on success and false on failure. The PHP manual recommends strict comparison when checking the return value. You can read the official reference for simplexml_load_file().
When should you use a PHP RSS feed reader?
Use a PHP RSS feed reader when you want to show recent content from an RSS feed on your website. This can be useful for blog updates, news feeds, product announcements, release notes, or any content source that publishes RSS.
An RSS feed is an XML document. So the parsing part is similar to reading any other XML file. The main difference is the structure. RSS usually has a <channel> node and multiple <item> nodes.
| RSS field | Meaning |
|---|---|
channel/title |
The feed title. |
channel/link |
The main website URL. |
channel/description |
A short description of the feed. |
channel/item/title |
The title of a feed item. |
channel/item/link |
The URL of a feed item. |
channel/item/description |
A short summary of the feed item. |
channel/item/pubDate |
The published date of the feed item. |
Example RSS feed file
The example project uses a local RSS file for reliable testing. This avoids depending on a remote feed while learning the code.
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>PHPpot Tutorials</title>
<link>https://phppot.com/</link>
<description>Latest tutorials from PHPpot.</description>
<item>
<title>PHP XML Parser Tutorial</title>
<link>https://phppot.com/php/php-xml-parser/</link>
<description>Learn how to parse XML in PHP using event handlers.</description>
<pubDate>Mon, 20 Apr 2026 10:30:00 +0530</pubDate>
</item>
<item>
<title>PHP SimpleXML Parser</title>
<link>https://phppot.com/php/php-simplexml-parser/</link>
<description>Read XML files and strings in PHP using SimpleXML.</description>
<pubDate>Tue, 21 Apr 2026 11:15:00 +0530</pubDate>
</item>
<item>
<title>PHP XMLReader Example</title>
<link>https://phppot.com/php/php-xml-reader/</link>
<description>Read large XML files in PHP using XMLReader.</description>
<pubDate>Wed, 22 Apr 2026 09:45:00 +0530</pubDate>
</item>
</channel>
</rss>
Save this file as sample-feed.xml. The PHP code will read this RSS file and list the feed items.
Create the RSS feed reader class
The RSS feed reader class loads the feed with simplexml_load_file(). It checks for parsing errors, validates the basic RSS structure, and returns the channel details and feed items as a PHP array.
This keeps the parsing logic separate from the HTML display code.
<?php
declare(strict_types=1);
final class RssFeedReader
{
public function read(string $feedPath): array
{
libxml_use_internal_errors(true);
$rss = simplexml_load_file($feedPath);
if ($rss === false) {
$errors = libxml_get_errors();
$firstError = $errors[0]->message ?? 'Unable to read RSS feed.';
libxml_clear_errors();
throw new RuntimeException(trim($firstError));
}
libxml_clear_errors();
if (!isset($rss->channel)) {
throw new RuntimeException('Invalid RSS feed. Channel node is missing.');
}
$items = [];
foreach ($rss->channel->item as $item) {
$items[] = [
'title' => (string) $item->title,
'link' => (string) $item->link,
'description' => $this->shorten((string) $item->description, 140),
'pubDate' => $this->formatDate((string) $item->pubDate),
];
}
return [
'channelTitle' => (string) $rss->channel->title,
'channelLink' => (string) $rss->channel->link,
'channelDescription' => (string) $rss->channel->description,
'items' => $items,
];
}
private function shorten(string $text, int $limit): string
{
$text = trim(strip_tags($text));
if (mb_strlen($text) <= $limit) {
return $text;
}
return mb_substr($text, 0, $limit - 3) . '...';
}
private function formatDate(string $date): string
{
if ($date === '') {
return '';
}
try {
return (new DateTimeImmutable($date))->format('M j, Y');
} catch (Exception) {
return $date;
}
}
}
?>
The read() method returns the channel title, channel link, channel description, and feed items. Each item contains the title, link, short description, and formatted publish date.
The shorten() method removes HTML tags from the description and trims long text. The formatDate() method converts the RSS date into a simple display format.
Display RSS feed items in HTML
After creating the RSS feed reader class, include it in index.php. Then read the RSS file and display the channel details and feed items.
Use htmlspecialchars() before printing feed values in the browser. RSS content may come from another website, so it should be treated as external input.
<?php
declare(strict_types=1);
require_once __DIR__ . '/RssFeedReader.php';
$feed = [];
$errorMessage = '';
try {
$reader = new RssFeedReader();
$feed = $reader->read(__DIR__ . '/sample-feed.xml');
} catch (RuntimeException $exception) {
$errorMessage = $exception->getMessage();
}
function e(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP RSS Feed Reader Demo</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main class="page">
<section class="card">
<h1>PHP RSS Feed Reader Demo</h1>
<p>This example reads an RSS feed with SimpleXML and displays feed items.</p>
<?php if ($errorMessage !== '') : ?>
<div class="message error">
<?php echo e($errorMessage); ?>
</div>
<?php elseif (!empty($feed)) : ?>
<div class="feed-header">
<h2>
<a href="<?php echo e($feed['channelLink']); ?>" target="_blank" rel="noopener">
<?php echo e($feed['channelTitle']); ?>
</a>
</h2>
<p><?php echo e($feed['channelDescription']); ?></p>
</div>
<div class="feed-list">
<?php foreach ($feed['items'] as $item) : ?>
<article class="feed-item">
<h3>
<a href="<?php echo e($item['link']); ?>" target="_blank" rel="noopener">
<?php echo e($item['title']); ?>
</a>
</h3>
<?php if ($item['pubDate'] !== '') : ?>
<p class="date"><?php echo e($item['pubDate']); ?></p>
<?php endif; ?>
<p><?php echo e($item['description']); ?></p>
</article>
<?php endforeach; ?>
</div>
<?php endif; ?>
</section>
</main>
</body>
</html>
Example output
When the page runs successfully, the browser shows the RSS channel title and a list of feed items.

RSS feed reader output displaying feed items parsed with PHP SimpleXML.
How the RSS feed reader code works
The code starts by enabling internal libxml errors. This lets the script handle RSS parsing errors instead of showing raw warnings in the browser.
libxml_use_internal_errors(true);
Then it loads the RSS feed with simplexml_load_file().
$rss = simplexml_load_file($feedPath);
If the feed cannot be loaded, the code gets the first libxml error and throws a clear exception.
if ($rss === false) {
$errors = libxml_get_errors();
$firstError = $errors[0]->message ?? 'Unable to read RSS feed.';
libxml_clear_errors();
throw new RuntimeException(trim($firstError));
}
After that, the code checks whether the RSS feed has a <channel> node. A normal RSS feed should have this node.
if (!isset($rss->channel)) {
throw new RuntimeException('Invalid RSS feed. Channel node is missing.');
}
The feed items are available inside $rss->channel->item. The code loops through each item and stores the values in a PHP array.
foreach ($rss->channel->item as $item) {
$items[] = [
'title' => (string) $item->title,
'link' => (string) $item->link,
'description' => $this->shorten((string) $item->description, 140),
'pubDate' => $this->formatDate((string) $item->pubDate),
];
}
The description may contain HTML. The shorten() method removes HTML tags and limits the length of the text.
private function shorten(string $text, int $limit): string
{
$text = trim(strip_tags($text));
if (mb_strlen($text) <= $limit) {
return $text;
}
return mb_substr($text, 0, $limit - 3) . '...';
}
The RSS publish date is usually in RFC 2822 format. The DateTimeImmutable class can parse this date and convert it into a simpler display format.
private function formatDate(string $date): string
{
if ($date === '') {
return '';
}
try {
return (new DateTimeImmutable($date))->format('M j, Y');
} catch (Exception) {
return $date;
}
}
Finally, the HTML page prints the channel and item values. Each value is escaped with htmlspecialchars() before it is shown in the browser.
function e(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
Read a remote RSS feed
The demo project uses a local RSS file so the example works even without internet access. In a real project, you may want to read a remote RSS feed URL.
For example, to read the PHPpot RSS feed, replace the local file path with the feed URL.
$feed = $reader->read('https://phppot.com/feed/');
This works when remote URL access is allowed in your PHP configuration. But for production use, it is better to fetch remote feeds with a proper HTTP client or a controlled cURL request. That gives you better control over timeout, redirects, response size, and errors.
Common errors and fixes
RSS feed reading errors are usually caused by invalid XML, unavailable feed URLs, missing PHP extensions, or unsafe output. These are the common issues to check first.
simplexml_load_file() returns false
This can happen when the feed URL is unavailable, the local file path is wrong, or the RSS XML is invalid.
if ($rss === false) {
$errors = libxml_get_errors();
$firstError = $errors[0]->message ?? 'Unable to read RSS feed.';
libxml_clear_errors();
throw new RuntimeException(trim($firstError));
}
First test with a local RSS file. After that, switch to a trusted remote feed URL.
Channel node is missing
A normal RSS feed should have a <channel> node. If the channel node is missing, the XML may not be a valid RSS feed.
if (!isset($rss->channel)) {
throw new RuntimeException('Invalid RSS feed. Channel node is missing.');
}
Feed description contains HTML
Many RSS feeds include HTML inside the description. If you want plain text, remove the tags before displaying it.
$description = trim(strip_tags((string) $item->description));
Feed dates are not formatted properly
RSS feed dates can vary. Use DateTimeImmutable inside a try-catch block so that invalid dates do not break the page.
try {
$date = (new DateTimeImmutable($itemDate))->format('M j, Y');
} catch (Exception) {
$date = $itemDate;
}
Security considerations
RSS feeds are external content. Even when the feed comes from a trusted website, treat the values as input that must be handled carefully.
- Escape feed values before printing them in HTML.
- Validate remote feed URLs before loading them.
- Use timeout control when reading remote feeds.
- Limit the number of feed items displayed on the page.
- Do not show raw parser errors or server paths in production.
- Cache remote feeds instead of requesting them on every page load.
The example uses htmlspecialchars() before showing feed values in the browser.
echo htmlspecialchars($item['title'], ENT_QUOTES, 'UTF-8');
If you allow users to submit feed URLs, do not load those URLs directly. Validate the URL, restrict allowed domains if possible, and use server-side timeout and size limits.
If your application reads XML from outside systems, it is also useful to understand XML-related risks such as XXE. The OWASP page on XML External Entity processing explains this issue in detail.
Developer FAQ
What is an RSS feed reader in PHP?
A PHP RSS feed reader reads an RSS XML feed and displays the feed items on a webpage. It usually reads the channel title, item title, link, description, and publish date.
Can I read an RSS feed with SimpleXML?
Yes. RSS is XML, so SimpleXML can read RSS feeds easily. Use simplexml_load_file() for a local RSS file or a trusted remote RSS URL.
How do I loop through RSS feed items in PHP?
After loading the RSS feed, loop through $rss->channel->item.
foreach ($rss->channel->item as $item) {
echo (string) $item->title;
}
Can I read a remote RSS feed URL?
Yes, if your PHP setup allows remote URL access. For production, it is better to fetch the remote feed with cURL or a controlled HTTP client so you can set timeouts and handle errors better.
Should I cache RSS feed results?
Yes. If you display a remote RSS feed on a live website, cache the result for a short time. This avoids slow page loads and prevents repeated requests to the feed source.
Why does the RSS description contain HTML?
Many RSS feeds include HTML inside the description. Use strip_tags() if you want to show a plain text summary.
$description = trim(strip_tags((string) $item->description));
Conclusion
A PHP RSS feed reader is a practical way to read and display updates from an RSS XML feed. With SimpleXML, you can load the feed, read the channel details, loop through feed items, and show titles, links, descriptions, and dates.
For learning and testing, a local sample RSS file is easier and more reliable. For production, use a trusted remote feed URL with timeout handling, validation, output escaping, and caching.
The downloadable example below gives you a complete working RSS feed reader project. It reads a sample RSS feed, handles parsing errors, formats dates, shortens descriptions, escapes output, and displays feed items in a clean layout.
Download source code
Download the PHP RSS feed reader example project and run it in your local PHP environment.
how do we get image from rss feed?
RSS images are not always stored in the same place. First check your feed XML.
If the feed has an enclosure image:
$enclosure = $item->enclosure;
if ($enclosure && strpos((string) $enclosure[‘type’], ‘image/’) === 0) {
$imageUrl = (string) $enclosure[‘url’];
}
If the image is inside the description HTML:
preg_match(‘/
]+src=[“\\’]([^”\\’]+)[“\\’]/i’, (string) $item->description, $matches);
$imageUrl = $matches[1] ?? ”;
Then echo it safely with htmlspecialchars().
Perfect! Thank you very much!
Welcome Freda
Hello, The readout works fine. How can still cleanly display the time of the feed in the code?
Thank you. You can use the RSS item’s pubDate value and format it with DateTime.
$publishedDate = new DateTime((string) $item->pubDate);
echo $publishedDate->format(‘M d, Y h:i A’);
For only the time, use:
echo $publishedDate->format(‘h:i A’);
This will show the feed time in a clean readable format.