SimplePie CakePHP Component

This article is also available in the following languages:
By 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:

<?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:

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

View Template:

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

Comments

  • Posted 07/26/10 02:47:50 AM
    Hi, I'm a new cake user so I appreciate your patience. I have a couple of questions.

    1) I'm trying to do a basic setup using a static feed address (as above) and I get an undefined variable for items in the view and therefore an invalid argument for the foreach. I'm using cake 1.2.7 and the only download of simplepie I seem to be able to get my hands on 1.2. I am slightly uncertain as to whether I've named the component file correctly 'simplepie.php', same as the file that goes in the vendors directory, but I don't think I would get as far as I have if I had done that wrong.

    2) In general, my intention is to have a mysql table with a list of blog rss feeds which includes basic info such as title but also a snippet of text plus time and date from the most recent post. Where am I actually running logic that associates said snippet with my mysql table -- should that be in the model? There are some allusions in the cookbook as to the potential of a model being an rss feed but I haven't seen any practical examples. Plus the specific feeds which are being accessed vary according to what's in the list of feeds. It's not like I want a single rss feed to neatly populate a table -- it's more like I have a feed address in a mysql row and I want that to be the basis for two other rows to become populated.

    Any advice on either point would be greatly appreciated.
  • Posted 04/28/10 08:39:23 AM
    Thanks for this code!!
    For Cake 1.3 replace the Component's line
    $folder->mkdir($this->cache);
    with
    $folder->create($this->cache);
  • Posted 02/24/10 10:10:40 PM
    While using this code on some of the latest cakePHP 1.2.5 I found a couple of issues:
    First one was explained above - add the following before the class:

    App::import('Vendor', 'Simplepie', array('file' => 'simplepie/simplepie.php'));

    The second one is regarding the cache - correct the following, from:
    $folder->mkdirr($this->cache);  to:
    $folder->mkdir($this->cache); 
    This should make it work perfectly!
  • Posted 12/09/09 03:24:37 AM
    I am trying to use Simplepie, when I am trying run the application, Its giving me a fatal Error, "Fatal error: Call to undefined method Folder::mkdirr() in E:\website\app\controllers\components\Simplepie.php on line 27"

    I am new to cake,

    Does anyone know the solution for this please?
    • Posted 12/14/09 08:09:06 AM
      all you need to do is change from mkdirr -> mkdir

      I am trying to use Simplepie, when I am trying run the application, Its giving me a fatal Error, "Fatal error: Call to undefined method Folder::mkdirr() in E:\website\app\controllers\components\Simplepie.php on line 27"

      I am new to cake,

      Does anyone know the solution for this please?
  • Posted 10/27/09 09:57:27 AM
    If you make the changes Boaz talks the component works perfectly.

    Thks.
  • Posted 09/25/09 12:31:46 PM
    In the component, it should be mkdir instead of mkdirr.
    And vendor() is deprecated. App::import('Vendore', ...) should be used.
  • Posted 04/07/09 12:56:40 AM
    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 03/30/09 10:39:57 AM
    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 08/30/08 07:41:05 AM
    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 06/28/08 08:54:08 AM
    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 01/25/08 07:48:58 AM
    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 09/30/07 09:02:35 AM
    IMHO the feed function should return $feed not $items, then you can read feed information like $feed->get_title()

Comments are closed for articles over a year old