Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

HOWTO: View an RSS feed from within Silverstripe


Reply


6 Posts   3628 Views

Avatar
mobius

Community Member, 54 Posts

12 March 2010 at 11:09am

Hey guys,

I've just tackled this issue and I thought I'd share my methods so people can build on what I've started.

An external web service our customer is using provides account details via RSS. Rather than having the user have to set up an RSS feed with the correct url parameters etc on each computer he uses, I've made a RSS reader that lets you output the data with your template.ss files in any way you want.

The rss reader wraps around [url]http://simplepie.org[/url]. At the moment I've only got the basic things I've needed available to the template files, but its really easy to add extra ones needed, based on the [url=http://simplepie.org/wiki/reference/start]SimplePie API[/url].

Lets get started!

First, download the latest SimplePie package. I used [url]http://github.com/rmccue/SimplePie/tarball/1.2[/url] for this example. Upload simplepie.inc to a convenient folder in your site, and add the following to _config.php:

require_once('path/to/simplepie.inc')

Now create a file called SimplePieResult.php in mysite/code/. Copy and paste the following contents into it:

<?php
/*
* Provides a wrapper around @see http://simplepie.org
*
* require_once('path/to/simplepie.inc') must be included in a _config.php somewhere
*/
class SimplePieResult extends ViewableData {
   protected $simplePie;
   protected $items;
   
   public function __construct($url) {
      parent::__construct();
      
      $this->simplePie = new SimplePie();
      $this->simplePie->enable_cache(false);
      $this->simplePie->set_feed_url($url);
      $this->simplePie->init();
      $this->simplePie->handle_content_type();
      
      if ($this->simplePie->data) {
         $this->items = new DataObjectSet();
         foreach ($this->simplePie->get_items() as $item) {
            $this->items->push(new SimplePieResult_Item($item));
         }
      }
   }
   public function Title() {
      return $this->simplePie->get_title();   
   }
   public function Items() {
      return $this->items;   
   }
   
   /* This is just a lazy function to allow people to configure stuff that hasn't been
    * programmed in yet
    *
    * Usage: $this->Configure('simplepie_function_name', 'function arg',..)
    */
   public function Configure() {
      $args = function_get_args();
      
      $method = array_shift($args);
      
      if (method_exists($this->simplePie, $method)) {
         return $this->simplePie->$method(implode(', ', $args));
      } else {
         user_error("The method $method does not exist on SimplePie", E_USER_ERROR);   
      }
   }
}
class SimplePieResult_Item extends ViewableData {
   public $Content;
   public $Date;
   public $Title;
   public $Link;
   
   public function __construct($item = null) {
      if ($item) {
         $this->Content = $item->get_content();
         $this->Date = $item->get_date();
         $this->Title = $item->get_title();
         $this->Link = $item->get_permalink();
      }
   }
}
?>

Now you can use this anywhere!

To use it, have a function in your page or wherever that does something like this:

function ViewRSS() {
return new SimplePieResult('http://url/to/my/feed');
}

Then your template might look something like this:

<% if ViewRSS %>
<% control ViewRSS %>
<% if Items %>
<h3>Recent txts and account details</h3>
<ul class="item">
<% control Items %>
   <li class="$EvenOdd"><h3>$Title</h3><p>$Content</p></li>
<% end_control %>
</ul>
<% else %>
<div class="bad">
   <p>The rss feed appears to be empty</p>
</div>
<% end_if %>
<% end_control %>
<% end_if %>

Avatar
mattclegg

Community Member, 56 Posts

4 May 2010 at 11:22pm

I had some weird filetype issues with the simplepie.inc which prevented editing the file in Aptana (over sftp), but aside from that I found running it a little slow. Im sure there has to be a better way of doing it. Adding some extra functionailty;

Restrict the number of feeds called by;

public function __construct($url,$limit=1) {
...
foreach ($this->simplePie->get_items() as $item) {
$this->items->push(new SimplePieResult_Item($item));
if(count($this->items)==$limit)
   break;
}

and use Silverstripe functions for data parsing
$this->Content = new Text();
$this->Content->setValue($item->get_content());
$this->Date = new Date();
$this->Date->setValue($item->get_date());

Hopefully this helps some one

Avatar
mobius

Community Member, 54 Posts

5 May 2010 at 8:28am

Cool. Yeah it can be a little slow depending on the remote link speed (the time it takes for your webserver to fetch the remote rss feed). There are caching methods in simplepie that might be worthwhile to investigate, but I only needed a simple rss fetcher so didn't implement any more of that.

If there's enough of a demand for this, I could turn it into a module, but I haven't any further dev needs for it (at the moment anyway).

Avatar
kuenkuen82

Community Member, 41 Posts

8 November 2010 at 11:04pm

I'm trying to extend the SimplePie_Item Class

Similar to [url]http://net.tutsplus.com/articles/news/extending-simplepie-to-parse-unique-rss-feeds/[/url]

but when I add $item->get_embed_id();

it doesn't find the the function, what am I missing?

class SimplePieResult_Item extends ViewableData {
public $Content;
public $Date;
public $Title;
public $Link;
   
    public $EmbedID;

public function __construct($item = null) {
if ($item) {
$this->Content = $item->get_content();
$this->Date = $item->get_date();
$this->Title = $item->get_title();
$this->Link = $item->get_permalink();
            
   //$this->EmbedID = $item->get_embed_id();
}
}
}

Avatar
Sphere

Community Member, 44 Posts

11 April 2011 at 10:51pm

It's easier to just do this:
Add to page.php

public function RSS($url){
$rss = new RestfulService('$url');
$connect = $rss->request();
$result = $connect->simpleXML($connect->getBody());
$output = '';
foreach($result as $key => $value){
$shorttext = str_replace('&amp;quot;', '&quot;', $value->description);
$output .= '</i><a href="' . $value->link . '" rel="external">' . $value->title . '</a><br />' . $value->description . '</li>';
}
return $output;
}

And in your template, wherever you want your RSS-feed, add $RSS('YourURL');

Avatar
becca

Community Member, 5 Posts

18 May 2011 at 2:14pm

Thanks for this, mobius! Very helpful.