Page 1 of 1

RSS displaying multiple feeds in time/date published order

Posted: Sat Jan 21, 2012 2:55 am
by dareskog
I'm trying to create a php rss parser that takes on multiple feeds, takes 10 entries from each, these all then get mixed together in an array and displayed in date order.
Atm i've got this:
[syntax=php]
<?php
require_once 'magpierss/rss_fetch.inc';
$num_items = 10;
$urls = array( "http://url1.com", "http://url2.com", "http://url3.com" );

foreach ($urls as $url) {
$rss = fetch_rss($url);
$items = array( $rss );
$items = array_slice($rss->items, 0, $num_items);

foreach ( $items as $item ) {
$title = $item[title];
$url = $item[link];
echo "<a href=$url>$title</a><br />";
}
}
?>
[/syntax]
The only bit I've got left to do is have them displayed in a collaborated date order but don't know how to get this done :(. I'm a new comer to php and have gone nuts trying to sort this out, so any help/examples would be awesome!
Cheers =]

Re: RSS displaying multiple feeds in time/date published ord

Posted: Sun Jan 22, 2012 2:43 pm
by jacek
Well one thing, you can remove this line

[syntax=php]$items = array( $rss );[/syntax]
since the one below it will overwrite it.

As for the sorting thing, you need to read all of the article you are going to show into one array and they sort that. So your code will probably look more like this

[syntax=php]<?php
require_once 'magpierss/rss_fetch.inc';

$num_items = 10;
$urls = array( "http://url1.com", "http://url2.com", "http://url3.com" );

$items = array();

foreach ($urls as $url) {
$rss = fetch_rss($url);
$items = array_merge($items, array_slice($rss->items, 0, $num_items));
}

// do sorting on $items

foreach ( $items as $item ) {
$title = $item[title];
$url = $item[link];
echo "<a href=$url>$title</a><br />";
}
?>[/syntax]

For the sorting then you probably need to use a usort(), have a look at http://php.net/usort for some examples.

Re: RSS displaying multiple feeds in time/date published ord

Posted: Tue Jan 24, 2012 11:47 pm
by dareskog
Ah cheers! I was able to work out a sorting method using the link you provided, took me a while to get it tho!
I've just got one last question which I'm struggling with.
ATM, I've got this:
[syntax=php]
$num_items = 8;
$urls = array( "http://www.url1.com", "http://www.url2.com", "http://www.url3.com" );

$items = array();
foreach ($urls as $url) {
$rss = fetch_rss($url);
$items = array_merge($items, array_slice($rss->items, 0, $num_items));
}

usort($items, 'date_cmp');

foreach ( $items as $item ) {
$title = $item[title];
$url = $item[link];
echo "- <a href=$url>$title</a><br />";
}

function date_cmp($a, $b)
{
$atime = (empty($a['date_timestamp']))
? strtotime($a['dc']['date'])
: $a['date_timestamp'];

$btime = (empty($b['date_timestamp']))
? strtotime($b['dc']['date'])
: $b['date_timestamp'];

if ($atime == $btime)
return 0;

return ($atime > $btime) ? -1 : 1;
}
[/syntax]

My Question is, is there any way of identifying from the individual rss outputs which url they originated from?
So this could then lead me on to being able to somehow detect which rss output came from which url rss feed and therefore displaying a corresponding picture.
E.G say one of the rss feeds was (i know this isn;t a real rss feed btw lol) www.google.com/rss, for each rss feed that got outputed originating from this url would have the google favicon next to it.
Is this possible?
Cheers again =]

Re: RSS displaying multiple feeds in time/date published ord

Posted: Sat Jan 28, 2012 10:40 pm
by jacek
It might be, the best thing to do would be to look at the source of the feed and see if they all have a property in their header that states the url. I don't think it is a required filed for RSS but they might all have it.