PHP RSS Feed Read and List

by Vincy. Last modified on July 6th, 2023.

PHP provides a simplexml_load_file() function for reading data from XML files. We have seen this function in the Simple XML Parser tutorial.

Using this function, we will read RSS feeds by passing the feed URL to this function. In this tutorial, we are parsing the RSS feed and splitting it into an object array.

By iterating this object array, we are getting the required property of each feed item to display.

View Demo

PHP Code for Reading and Listing XML Feeds

This shortcode is used to parse XML feed URLs to read and display data items.

php-rss-read

<?php
$rss_feed = simplexml_load_file("https://phppot.com/feed/");
if (! empty($rss_feed)) {
    $i = 0;
    foreach ($rss_feed->channel->item as $feed_item) {
        if ($i >= 10)
            break;
        ?>
<div>
	<a class="feed_title" href="<?php echo $feed_item->link; ?>"><?php echo $feed_item->title; ?></a>
</div>
<div><?php echo implode(' ', array_slice(explode(' ', $feed_item->description), 0, 14)) . "..."; ?></div>
<?php
        $i ++;
    }
}
?>

RSS Feed List HTML

This HTML table will display a list of RSS feed items by iterating the item array.

<table class=”rss-table”> <tbody> <tr> <th><strong>PhpPot RSS Feed</strong></th> </tr> <?php //Loop starts for iterating feed item array ?> <tr> <td valign=”top”> <div><a class=”feed_title” href=”<?php echo $feed_item->link; ?>”><?php echo $feed_item->title; ?></a></div> <div><?php echo implode(‘ ‘, array_slice(explode(‘ ‘, $feed_item->description), 0, 14)) . “…”; ?></div> </td> </tr> <?php // Loop ends ?> </tbody> </table>

View DemoDownload

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page