Using SimplePie to parse RSS
May 8th, 2006 by Sarah KingCake PHP have launched a new version or their PHP framework and one of the snippets was the integration of SimplePie. The install and demo for normal sites is incredibly simple and the code is fully featured but held in a single file. It can even spit out the "subscribe via" links for the major sites.
All in all it appears to be very impressive.
- Unzip the files on your local file system and run the demo and make sure everything is setup ok. Don't be alarmed by the huge logo, you are still on your own site
- If you want to skip the demo the only file you actually need is simplepie.inc
- create this function in your script
PHP:
-
function getSimplePie($url)
-
{
-
include('simplepie/simplepie.inc');
-
// Parse it
-
$feed = new SimplePie();
-
$feed->feed_url($url);
-
$feed->cache_location('./../cache');
-
$feed->init();
-
-
if ($feed->data)
-
{
-
$output = "<table cellspacing='0' cellpadding='0'>
-
<tr><th valign='top'>
-
<a href=" . $feed->get_feed_link() . ">"
-
.$feed->get_feed_title()
-
."</a> - ".$feed->get_feed_description()
-
."</th></tr>";
-
-
for ($x = 0; $x <$max; $x++)
-
{
-
$output .= "<tr>
-
<td>
-
<a href='" . $feed->get_item_permalink($x) . "'>"
-
.$feed->get_item_title($x)
-
."</a>
-
</td>\n";
-
}
-
}
-
$output .= "</table>";
-
return $output;
-
}//getSimplePie
-
- You can now echo getSimplePie and pass in the feed address from anywhere in your page.


[...] SimplePie [...]
Where is the CakePHP SimplePie snippet? I just can’t find it.
Hi Geoffrey, I picked it up here: http://cakeforge.org/snippet/detail.php?type=snippet&id=53
Why the tables?
In the example? That’s just a hangover from the original test. I’d use a list normally.
How do i echo getsimplepie?
just
[php]echo getSimplePie(‘http://mysite.com/feed/’);[/php]
is there a way to include more than one rss feed?
No, but you can get several feeds – build them into an array, sort and output.