Instantly share code, notes, and snippets.

Embed
What would you like to do?
RSS Feed Reader in PHP
<html>
<head>
<title>RSS Feed Reader</title>
</head>
<body>
<?php
//Feed URLs
$feeds = array(
"http://maxburstein.com/rss",
"http://www.engadget.com/rss.xml",
"http://www.reddit.com/r/programming/.rss"
);
//Read each feed's items
$entries = array();
foreach($feeds as $feed) {
$xml = simplexml_load_file($feed);
$entries = array_merge($entries, $xml->xpath("//item"));
}
//Sort feed entries by pubDate
usort($entries, function ($feed1, $feed2) {
return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
});
?>
<ul><?php
//Print all the entries
foreach($entries as $entry){
?>
<li><a href="<?= $entry->link ?>"><?= $entry->title ?></a> (<?= parse_url($entry->link)['host'] ?>)
<p><?= strftime('%m/%d/%Y %I:%M %p', strtotime($entry->pubDate)) ?></p>
<p><?= $entry->description ?></p></li>
<?php
}
?>
</ul>
</body>
</html>
@kudataz

This comment has been minimized.

kudataz commented Feb 20, 2015

how to display max 5 items?

@UnXman

This comment has been minimized.

UnXman commented Jan 10, 2016

Good script. Works for a simple application

@pascaldulieu

This comment has been minimized.

pascaldulieu commented Oct 2, 2016

How can i take the data output and display it in my index.html page?

@marcelmarnix

This comment has been minimized.

marcelmarnix commented Apr 9, 2018


<?php

$NUMITEMS   = 2;

$count = 0;
        //Print all the entries
        foreach($entries as $entry){
        ?>
            <li><a href="<?= $entry->link ?>"><?= $entry->title ?></a> (<?= parse_url($entry->link)['host'] ?>)
            <p><?= strftime('%m/%d/%Y %I:%M %p', strtotime($entry->pubDate)) ?></p>
            <p><?= $entry->description ?></p></li>
       <?php
        }
        if(++$count >= $NUMITEMS) break;
        ?>
@kevinlingier1

This comment has been minimized.

kevinlingier1 commented May 12, 2018

<?php

$NUMITEMS   = 2;

$count = 0;
        //Print all the entries
        foreach($entries as $entry){
        ?>
            <li><a href="<?= $entry->link ?>"><?= $entry->title ?></a> (<?= parse_url($entry->link)['host'] ?>)
            <p><?= strftime('%m/%d/%Y %I:%M %p', strtotime($entry->pubDate)) ?></p>
            <p><?= $entry->description ?></p></li>
       <?php
        
        if(++$count >= $NUMITEMS) break;
        }
        ?>

} <-- it must be befor the close php tag ?> else it would not count and break the foreach rule.

@multiomer1997

This comment has been minimized.

multiomer1997 commented Oct 16, 2018

Can some help me with add a image with this code from the RSS Feed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment