SimplePie CakePHP Component

By Matt Curry aka "mattc"
SimplePHP is a PHP class for retrieval and parsing of RSS feeds. This is a wrapper to that class making it easy to use in the CakePHP framwork. Much of this component is taken from the work of Scott Sansoni (http://cakeforge.org/snippet/detail.php?type=snippet&id=53). This is mostly an update so the component works with the lastest version of SimplePie.

Download

You can download a zip of the component at http://sandbox.pseudocoder.com/demo/simplepie.

Usage


  1. CakePHP either 1.1 or 1.2
  2. Download SimplePie 1.0.1 (http://simplepie.org/downloads) and unzip the contents. Move the simplepie.inc to one of the vendors folders. Rename the file to simplepie.php. I like to put the file in the sub folder with the README.txt and LICENSE.txt for easy reference.
  3. Download the component (or paste the full code from below) and unzip it to app/controllers/components.
  4. Include the component in any controller that will need it.


Component Class:

Download code <?php <?php
/*
 * SimplePie CakePHP Component
 * Copyright (c) 2007 Matt Curry
 * www.PseudoCoder.com
 *
 * Based on the work of Scott Sansoni (http://cakeforge.org/snippet/detail.php?type=snippet&id=53)
 *
 * @author      mattc <matt@pseudocoder.com>
 * @version     1.0
 * @license     MIT
 *
 */

class SimplepieComponent extends Object {
  var 
$cache;

  function 
__construct() {
    
$this->cache CACHE 'rss' DS;
  }

  function 
feed($feed_url) {
    
    
//make the cache dir if it doesn't exist
    
if (!file_exists($this->cache)) {
      
$folder = new Folder();
      
$folder->mkdirr($this->cache); 
    }

    
//include the vendor class
    
vendor('simplepie/simplepie');

    
//setup SimplePie
    
$feed = new SimplePie();
    
$feed->set_feed_url($feed_url);
    
$feed->set_cache_location($this->cache);

    
//retrieve the feed
    
$feed->init();

    
//get the feed items
    
$items $feed->get_items();

    
//return
    
if ($items) {
      return 
$items;
    } else {
      return 
false;
    }
  }
}
?>
?>


Controller Class:

Download code <?php $items $this->Simplepie->feed('http://feeds.feedburner.com/pseudocoder');
?>


View Template:

Download code foreach($items as $item) {
  echo $html->link($item->get_title(), $item->get_permalink()) . '<br />';
}

Comments 482

CakePHP team comments Author comments

Comment

1 feed function return

IMHO the feed function should return $feed not $items, then you can read feed information like $feed->get_title()
posted Sun, Sep 30th 2007, 09:02 by Spout

Question

2 Problem with SimplePie 1.1

First of all, sorry for my bad Englisch.

Great tutorial about integrating SimplePie in a cakePHP application.

SimplePie 1.0.1 cann't download any more at the website, only SimplePie 1.1. The new version gaves the warning:

Warning (2): preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 551 [COREappvendorsrsssimplepie.php, line 11599]
Warning (2): preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 497 [COREappvendorsrsssimplepie.php, line 11687]
Warning (2): preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 497 [COREappvendorsrsssimplepie.php, line 11753]

Does somebody now where I can downlaod SimplePie 1.0.1 or is there an update of the component for this problem?
posted Fri, Jan 25th 2008, 07:48 by Jochem

Login to Submit a Comment