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. | |
|
||||||||||||||||||||||||||||||
Download a raw-text version of this code by clicking on "Download Version"
<?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;
}
}
}
?>
You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..