SimplePie CakePHP Component

By Matt Curry (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 Sep 30, 2007 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 [CORE\app\vendors\rss\simplepie.php, line 11599] Warning (2): preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 497 [CORE\app\vendors\rss\simplepie.php, line 11687] Warning (2): preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 497 [CORE\app\vendors\rss\simplepie.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 Jan 25, 2008 by Jochem
 

Comment

3 Updates on vendor

I have used the latest version of SimplePie 1.1 and I am using CakePHP 1.2.x RC1.

On 1.2.x RC1, I got this error.
Warning (512): (vendor) Deprecated, see App::import('Vendor', '...'); [CORE\cake\basics.php, line 1123]
All you need to do is change the line (simplepie component):

from:

Component Class:

<?php 
vendor
('simplepie/simplepie');
?>

to:

Component Class:

<?php 
App
::import('Vendor''simplepie/simplepie');
?>

Hope this helps!
Posted Jun 28, 2008 by Louie Miranda
 

Comment

4 SimplePie Memory Leak

The current version of SimplePie suffers from a memory leak in php, this becomes a problem when you try to get a lot of feeds or feeds with lots of items, your server runs out of memory quickly..

There is a fix for this but I don't know how to apply it in the cakephp environment, here's the link: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
Any help with this would be appreciated.
Posted Aug 30, 2008 by Jelmer
 

Comment

5 session problem

Hello, nice component it seems to work well for me.
the only pb i have is i m loosing connexion infos (login, pass...)and i can"t find why.
any idea ?
Posted Mar 30, 2009 by mulot
 

Comment

6 Why not RSSHelper

CakePHP has a built-in helper for dealing with RSS content.

Very simple to use and exposes details like how it's suggested in comments here.
Posted Apr 7, 2009 by Ben