Search This Blog

Create RSS Agregator with SimplePie and CakePHP

Here it's my experiment creating RSS agregator using CakePHP and SimplePie. This code I found here, but I write again with some fixes for CakePHP 1.3.

The stuff:
-CakePHP v. 1.3
-xampp
-SimplePie, download trunk version from Github

Instructions:
1. Ectract SimplePie into /app/vendors directory
2. Create simplepie.php in /app/controller/components

<?php
class SimplepieComponent extends Object {
    var $cache;
   
    function __construct() {
        // cache engine
        $this->cache = CACHE . 'rss' . DS; // file cache mode

      }

      function feed($feed_url) {
       
        //make the cache dir if it doesn't exist
        if (!file_exists($this->cache)) {
          $folder = new Folder();
          $folder->create($this->cache);
        }
   
        //include the vendor class
        App::import('Vendor', 'simplepie/simplepieautoloader');
       
   
        //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;
        }
      }
}

3. Create simplepies_controller.php in /app/controllers directory:

<?php
class SimplepiesController extends AppController {
    var $uses = null;
    var $components = array(
        'Simplepie' => array(
            'name' => 'Simplepie'
        ),
    );
    function read() {
        $items = $this->Simplepie->feed('http://feeds.feedburner.com/galuhpakuan/jktI');
        $this->set('items', $items);
    }
}

4. Create read.ctp in /app/views/simplepies directory:

<?php

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


Example RSS agregate from galuhpakuan.com

I hope you found it useful :).

share and comment


Related Posts :