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 http://simplepie.org. 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 SimplePie API.
Lets get started!
First, download the latest SimplePie package. I used http://github.com/rmccue/SimplePie/tarball/1.2 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 %>