Browse | Submit New Snippet | Create Package

 

SimplePie Component

Type:
Sample Code (HOWTO)
Category:
Other
License:
GNU General Public License
Language:
PHP
 
Description:
Simple component to use SimplePie (http://www.simplepie.org) to grab an RSS or Atom feed and use it in your Cake projects. My component is really basic but I'm sure can be extended or modified for a heap of different uses.

Versions Of This Snippet::

Aaron H
Snippet ID Download Version Date Posted Author Delete
4000.12009-02-28 22:18Aaron H
Changes since last version::
2008/02/28

Updated to work with CakePHP 1.2 and SimplePie 1.1.3

Couple of improvements for usability
3410.022007-07-25 13:53brandon mardock
Changes since last version::
Made two simple changes to the function calls in the component to work with the changes made on simplepie's new version

changed:
$feed->feed_url($this->feedurl);
to:
$feed->set_feed_url($this->feedurl);

changed:
$feed->cache_location($this->cachelocation);
to:
$feed->set_cache_location($this->cachelocation);
800.0.12006-05-03 07:13Scott Sansoni

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :0.1

<?php
/*
 *   Copy simplepie.inc to the 'vendors' folder  (app/controllers/vendors)
 *   simplepie.php (this file) goes under app/controllers/components
 *
 *   Tested with SimplePie Version 1.1.3 (http://simplepie.org) & CakePHP 1.2
 *   SimplepieComponent
 * 
 *  @author Aaron - http://hypercas.com
 *  @version 0.1
 *  @license MIT
 *
*/
class SimplepieComponent extends Object
{
    var $sp_cache_location = null;            //Folder used to cache feeds
    var $sp_cache_active = null;              //To turn caching on/off use true/false
    var $sp_feed_url;                         //Can be used to set default fedd

    var $sp_feed;
    var $error_msg;

    function __construct()
    {
        if($this->sp_cache_location === null)$this->sp_cache_location = CACHE . 'simplepie' . DS;
        if($this->sp_cache_active === null)$this->sp_cache_active = true;
    }

    function feed($feed_url=null)
    {
        if(!$feed_url)$feed_url = $this->sp_feed_url;   //Replace null url will default
        else $this->sp_feed_url = $feed_url;            //Replace default url with latest url

        //Make sure the feed url is not empty
        if(!$feed_url)
        {
            $this->error_msg = 'Feed URL not present';
            return false;
        }

        //Create the cache dir if it doesn't exist
        if (!file_exists($this->sp_cache_location))
        {
            $folder = new Folder();
            $folder->mkdir($this->sp_cache_location);
        }

        //Import the vendor file
        App::import('vendor','simplepie',array('file'=>'simplepie.inc'));

        //Create a SimplePie instance
        $this->sp_feed = new SimplePie();
        $this->sp_feed->set_feed_url($feed_url);
        $this->sp_feed->set_cache_location($this->sp_cache_location);
        $this->sp_feed->enable_cache($this->sp_cache_active);

        //Retrieve the feed
        $this->sp_feed->init();

        //Get the feed items
        $items = $this->sp_feed->get_items();

        //Return result
        if ($items)
        {
            return $items;
        }
        else
        {
            $this->error_msg = $this->sp_feed->error();
            return false;
        }
    }
}
?>
		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..