This mirror of an article describes how to aggregate and collate RSS and Atom feeds with MagpieRSS. It’s really effective, but in following the steps exactly laid out, you lose channel information. When you want to distinguish between different feeds, you may want to include the basics, such as title (the name of the site) and link (the base url of the site.)

So I devised a bit of a hack to append that information to the items array.

Here’s the original code:

$urls = array(‘http://example.org/index.rdf’, …);

$items = array();

foreach ( $urls as $url ) {

    $rss = fetch_rss($url);

    if (!$rss) continue;

    $items = array_merge($items, $rss->items);

}

What you want to do is append the channel fields to each item of the $rss->items array. You’ll need to drill down to an individual item, append the new fields, then push the resulting array into a buffer, which then gets merged with items from other feeds. It’s somewhat kind of convulted, given the nested structure of the arrays.

The buffer also needs to be reset each time it’s used. It’s also wise to name the indeces for the fields channel_title and channel_link, so as not to conflict with the item title and the item link.

$urls = array(‘http://example.org/index.rdf’, …);

$items = array();

$channelItems = $rss->items;

$completeItems = array();

foreach ($channelItems as $item)

{

    $item[channel_title] = $rss->channel[title];

    $item[channel_link] = $rss->channel[link];

    array_push($completeItems, $item);

}

$items = array_merge($items, $completeItems);

unset($completeItems);

Now when you cycle through each item, you can include the channel title and link. The following is an example with Smarty:

{foreach item=feed from=$feed}

{if $feed.dc.creator}{assign var=author value=$feed.dc.creator}

{elseif $feed.author_name}{assign var=author value=$feed.author_name}

{/if}

<p>

<strong><a href=”{$feed.link}”>{$feed.title}</a></strong><br>

{if $feed.description}{$feed.description}<br>{/if}

<span style=”font-size: 10px;”>– posted on {$feed.date_timestamp|date_format:”%m/%d/%Y %H:%M:%S”} |

file under: <a href=”{$feed.channel_link}”>{$feed.channel_title}</a></span><br>

</p>

{/foreach}