May 8, 2006

Using SimplePie to parse RSS

Technical Discussions — Sarah King

Cake 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.

  1. 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 ;)
  2. If you want to skip the demo the only file you actually need is simplepie.inc
  3. create this function in your script
    PHP:
    1. function getSimplePie($url)
    2. {
    3.    include('simplepie/simplepie.inc');
    4.    // Parse it
    5.    $feed = new SimplePie();
    6.    $feed->feed_url($url);
    7.    $feed->cache_location('./../cache');
    8.    $feed->init();
    9.    
    10.    if ($feed->data)
    11.    {   
    12.       $output = "<table cellspacing='0' cellpadding='0'>
    13.       <tr><th valign='top'>
    14.       <a href=" . $feed->get_feed_link() . ">"
    15.       .$feed->get_feed_title()
    16.       ."</a> - ".$feed->get_feed_description()
    17.       ."</th></tr>";
    18.    
    19.       $max = min(5, $feed->get_item_quantity());
    20.       for ($x = 0; $x <$max; $x++)
    21.       {
    22.          $output .= "<tr>
    23.          <td>
    24.          <a href='" . $feed->get_item_permalink($x) . "'>"
    25.          .$feed->get_item_title($x)
    26.          ."</a>
    27.          </td>\n";
    28.       }
    29.    }
    30.    $output .= "</table>";   
    31.    return $output;
    32. }//getSimplePie

  4. You can now echo getSimplePie and pass in the feed address from anywhere in your page.

Also on this site

Tags: ,

9 Comments »

  1. [...] SimplePie [...]

    Pingback by Simple SEO » Adding RSS to your website — May 8, 2006 @ 2:47 pm

  2. Where is the CakePHP SimplePie snippet? I just can’t find it.

    Comment by Geoffrey Sneddon — May 11, 2006 @ 9:11 am

  3. Hi Geoffrey, I picked it up here: http://cakeforge.org/snippet/detail.php?type=snippet&id=53

    Comment by sarahk — May 11, 2006 @ 9:19 am

  4. Why the tables? :(

    Comment by EJ Fox — May 12, 2006 @ 9:13 am

  5. In the example? That’s just a hangover from the original test. I’d use a list normally.

    Comment by sarahk — May 12, 2006 @ 9:17 am

  6. How do i echo getsimplepie?

    Comment by Ashley — May 17, 2006 @ 11:37 pm

  7. just

    [php]echo getSimplePie(’http://mysite.com/feed/’);[/php]

    Comment by Sarah King — May 18, 2006 @ 7:34 am

  8. is there a way to include more than one rss feed?

    Comment by obay — June 21, 2007 @ 8:55 pm

  9. No, but you can get several feeds - build them into an array, sort and output.

    Comment by Sarah King — June 22, 2007 @ 2:52 am

Leave a comment

RSS feed for comments on this post. TrackBack URI

Pages

Categories:

Other Resources

Subscribe in NewsGator Online
GeoURL