Jump to:

5453 Posts in 1674 Topics by 1198 members

Customising the CMS

SilverStripe Forums » Customising the CMS » HOWTO: View an RSS feed from within Silverstripe

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3410 Views
  • mobius
    Avatar
    Community Member
    54 Posts

    HOWTO: View an RSS feed from within Silverstripe Link to this post

    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 %>

  • mattclegg
    Avatar
    Community Member
    56 Posts

    Re: HOWTO: View an RSS feed from within Silverstripe Link to this post

    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

  • mobius
    Avatar
    Community Member
    54 Posts

    Re: HOWTO: View an RSS feed from within Silverstripe Link to this post

    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).

  • kuenkuen82
    Avatar
    Community Member
    41 Posts

    Re: HOWTO: View an RSS feed from within Silverstripe Link to this post

    I'm trying to extend the SimplePie_Item Class

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

    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();
    }
    }
    }

  • Sphere
    Avatar
    Community Member
    44 Posts

    Re: HOWTO: View an RSS feed from within Silverstripe Link to this post

    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');

  • becca
    Avatar
    Community Member
    5 Posts

    Re: HOWTO: View an RSS feed from within Silverstripe Link to this post

    Thanks for this, mobius! Very helpful.

    3410 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.