Search code examples
rsssimplepie

Multiple feed is not working properly with SimplePie


I am trying to get multiple feeds from various sites. But whenever I run the script it gives me feeds from either one site and sometimes from all the sites specified in the script. Here is the code from Simplepie -

/ Include the SimplePie library
// For 1.0-1.2:
#require_once('simplepie.inc');
// For 1.3+:
require_once('autoloader.php');

// Create a new SimplePie object
$feed = new SimplePie();

$feed->set_feed_url(array(
'http://digg.com/rss/index.xml',
'http://feeds.tuaw.com/weblogsinc/tuaw',
'http://feeds.uneasysilence.com/uneasysilence/blog'
));

// We'll use favicon caching here (Optional)
$feed->set_favicon_handler('handler_image.php');

// Initialize the feed object
$feed->init();

// This will work if all of the feeds accept the same settings.
$feed->handle_content_type();

// Begin our XHTML markup
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head>
<title>Awesome feeds</title>
<link rel="stylesheet" href="../demo/for_the_demo/simplepie.css"
type="text/css" media="screen" charset="utf-8" />

<style type="text/css">
h4.title {
    /* We're going to add some space next to the titles so we can fit 
the 16x16 favicon image. */
    background-color:transparent;
    background-repeat:no-repeat;
    background-position:0 1px;
    padding-left:20px;
}
</style>
</head>
<body>
<div id="site">

    <?php if ($feed->error): ?>
    <p><?php echo $feed->error; ?></p>
    <?php endif; ?>

    <h1>Awesome feeds</h1>

    <?php foreach ($feed->get_items() as $item): ?>

    <div class="chunk">

        <?php /* Here, we'll use the $item->get_feed() method to gain access to the parent feed-level data for the specified item. */ ?>
        <h4 class="title" style="background-image:url(<?php $feed = $item->get_feed(); echo $feed->get_favicon(); ?>);"><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h4>

        <?php echo $item->get_content(); ?>

        <p class="footnote">Source: <a href="<?php $feed = $item->get_feed(); echo $feed->get_permalink(); ?>"><?php $feed = $item->get_feed(); echo $feed->get_title(); ?></a> | <?php echo $item->get_date('j M Y | g:i a T'); ?></p>

    </div>

    <?php endforeach; ?>

</div>

Does anybody have idea what is going wrong with the current script?? I searched for information related to this but I got no help.


Solution

  • What I do is limit the number of items per feed in my config code so that you get some from each feed. Because it sorts by date, if one feed has 15 items that are more recent than any of your other feeds, you'll get those first and you might not see any items from the other feeds.

    $max_items_per_feed = 3;
    // limit the number of items
    $feed->set_item_limit($max_items_per_feed);