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.

Archive

Our old forums are still available as a read-only archive.

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

RSS Parser


Go to End
Reply

9 Posts   7093 Views

Avatar
Decisive Flow

15 November 2006 at 4:40pm Community Member, 73 Posts

Has anyone ever made a RSS parser module? I would like to add the latest posts from team members blogs to our website.

:)

Avatar
Sam

15 November 2006 at 7:36pm Administrator, 685 Posts

No, but it's a good idea. We'll put one in the next version.

Alternatively, you could probably try and build one using magpie rss or something.

You'd need to create methods on the appropriate controller, something like this.

function MyRSSFeed() {
$posts = new DataObjectSet();
$rssFeed = getSomethingFromMagpie();

foreach($rssFeed as $item) {
$posts->push( new ArrayData(array(
"Title" => "something",
"Date" => "something else",
)));
}

return $posts;
}

Avatar
Ingo

15 November 2006 at 8:27pm Forum Moderator, 801 Posts

Or use Simplepie (http://www.simplepie.org) - it's a bit smarter/faster/up-to-date than Magpie.

Avatar
Decisive Flow

16 November 2006 at 2:08pm Community Member, 73 Posts

ummm.... I've got SimplePie working to a certain extent. My only problem is the HTML side of life - PHP doesn't seem to like being in the .ss files and I'm not sure how else to add it - it's this part:

<h1>
<a href="<?php echo $feed->get_feed_link(); ?>">
<?php echo $feed->get_feed_title(); ?>
</a>
</h1>
   
    <!-- Get the title of the news item and link it back to the original post -->
<h2>
<a href="<?php echo $item->get_permalink(); ?>">
<?php echo $item->get_title(); ?>
</a>
</h2>

<!-- Get the date that it was posted -->
<p><?php echo $item->get_date('j M Y'); ?></p>

<!-- Show the news posting -->
<p><?php echo $item->get_description(); ?></p>
<!-- Stop looping through each item once we've gone through all of them. -->
<?php } ?>

If i put it in the php file it shows up above the rest. What's the SS way of coping? :)

Avatar
Sam

17 November 2006 at 1:36pm (Last edited: 17 November 2006 1:37pm), Administrator, 685 Posts

Umm, this is a little tricky. You need to build a SilverStripe ViewableData class to provide the data to the template, that wraps around SimpleRSS.

Create a file called RSSReader.php and put this code in

class RSSReader extends ViewableData {
function __construct() {
$this->feed = (create $feed here)
}

functon Link() {
return $this->feed->get_feed_link();
}

function Title() {
return $this->feed->get_feed_title();
}

function Items() {
$items = new DataObjectSet();
foreach($this->feed->items as $item) {
$items->push( new RSSReader_Item( $item ) );
}
}
}

class RSSReader_Item extends ViewableData {
function __construct( $item) {
$this->item = $item;
}

function Title() {
return $this->item->get_title();
}
function Description() {
return $this->item->get_description();
}
}

2 Your SS template then looks like other SS templates. Call is RSSReader.ss, for example

<h1>
<a href="$Link">
$Title
</a>
</h1>

<% control Items %>
<!-- Get the title of the news item and link it back to the original post -->
<h2>
<a href="$Permalink">
$Title
</a>
</h2>

<!-- Get the date that it was posted -->
<p>$Date(j M Y)</p>

<!-- Show the news posting -->
<p>$Description</p>
<!-- Stop looping through each item once we've gone through all of them. -->
<% end_control %>

3. In the controller that you're going to put this feed on, you'll need to create a method to build one of these objects and render it using RSSReader.ss

function MyRSSFeed() {
$rss = new RSSReader();
return $rss->renderWith('RSSReader');
}

4. You can then put $MyRSSFeed into your template.

Avatar
sarahk

26 April 2007 at 12:39pm Community Member, 46 Posts

Finally got this working with a few coding changes. I'll list them below. Simplepie has changed it's syntax since I last used it. I referred to my own blog post and found bits are now wrong...!

No change to RSSReader.ss and it lives in the Templates folder (not subfolders)

I made this minor change to the code that goes in the controller - to allow me to use different feeds in different places. More flexibility will be needed and I might even add it to the page or holder definition in future.

   function MyRSSFeed() {
      $url = 'http://www.itamer.com/feed/';
      $rss = new RSSReader($url);
      return $rss->renderWith('RSSReader');
   }

And lots of changes to the code inside RSSReader.php

class RSSReader extends ViewableData {
   var $feed;
   
   function __construct($url) {
      $this->feed = new SimplePie($url);
      $this->feed->cache_location('./../../../cache'); // I wanted to specify where the cache is
      $this->feed->init();
   }
   
   function setFeed($feed)
   {
      $this->feed->feed_url = $feed;   
   }
   
   function Link()
   {
      return $this->feed->get_feed_link();
   }
   
   function Title() {
      return $this->feed->get_feed_title();
   }
   
   function Items() {
      
      $items = new DataObjectSet();
      foreach($this->feed->data['items'] as $item)
      {
         $items->push( new RSSReader_Item( $item ) );
      }
      return $items;
   }
}
   
class RSSReader_Item extends ViewableData {
   function __construct( $item) {
      $this->item = $item;
   }
   
   function Title() {
      return $this->item->get_title();
   }
   function Permalink() {
      return $this->item->get_permalink();
   }
   function Description() {
      return $this->item->get_description();
   }
}

You can see it working on http://www.andrewking.co.nz/pm/news/

Avatar
elijahlofgren

30 July 2007 at 6:13pm (Last edited: 1 August 2007 9:14am), Google Summer of Code Hacker, 222 Posts

I spent quite a while trying to get SimplePie working on my remove webserver, but was unabled to. I found the simple LastRSS class and was able to easily use it in SilverStripe by copying [url]http://lastrss.oslab.net/lastRSS.phps[/url] to mysite/code/lastRSS.php and adding this to class Page_Controller in mysite/code/Page.php:

   function LastRSSFeed($url) {
      // include lastRSS
      //include "./lastRSS.php";
      
      // Create lastRSS object
      $rss = new lastRSS;
      
      // Set cache dir and cache time limit (1200 seconds)
      // (don't forget to chmod cahce dir to 777 to allow writing)
      $rss->cache_dir = '../cache';
      $rss->cache_time = 1200;
      $output = '';
      // Try to load and parse RSS file
      if ($rs = $rss->get($url)) {
         // Show website logo (if presented)
         if (!empty($rs['image_url'])) {
            $output .= '<a href="'.$rs['image_link'].'"><img src="'.$rs['image_url'].'" alt="'.$rs['image_title'].'" /></a><br />'."\n";
         }
         // Show clickable website title
         $output .= '<h4><a href="'.$rs['link'].'">'.$rs['title'].'</a></h4>'."\n";
         // Show website description
         if (!empty($rs['description'])) {
            $output .= $rs['description']."<br />\n";
         }
         // Show last published articles (title, link, description)
         $output .= "<ul>\n";
         foreach($rs['items'] as $item) {
            // Fix quote problem
            if (!empty($item['description'])) {
               $item['description'] = str_replace('&#x26;quot;', '&quot;', $item['description']);
            } else {
               $item['description'] = '';
            }
            $output .= '<li><a href="'.$item['link'].'">'.$item['title']."</a><br />".$item['description']."</li>\n";
         }
         $output .= "</ul>\n";
      } else {
         $output .= "Error: It's not possible to reach RSS file...\n";
      }
      return $output;
   }

Then I simply put this in mysite/templates/Layout/HomePage.ss

$LastRSSFeed(http://del.icio.us/rss/elijahlofgren)

You can see it in use on [url]http://new.elijahlofgren.com/[/url] under "My Latest Del.icio.us Links".

It's not "the SilverStripe way", but it works. ;)

Edit: I also had to make this change to lastRSS to make it not give errors:

--- lastRSS.php 2004-10-26 13:04:52.000000000 -0500
+++ lastRSS-patched.php 2007-07-30 01:17:40.000000000 -0500
@@ -57,8 +57,10 @@
// If CACHE ENABLED
if ($this->cache_dir != '') {
$cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
- $timedif = @(time() - filemtime($cache_file));
- if ($timedif < $this->cache_time) {
+ if (is_file($cache_file)) {
+ $timedif = @(time() - filemtime($cache_file));
+ }
+ if (is_file($cache_file) && $timedif < $this->cache_time) {
// cached file is fresh enough, return cached array
$result = unserialize(join('', file($cache_file)));
// set 'cached' to 1 only if cached file is correct

Edit: There's a much better way! :) See [url=http://www.silverstripe.com/google-summer-of-code-forum/flat/2182?showPost=3084#post3084]Re: Zend Framework's implementation of Mashup Web Services[/url]

Avatar
laktek

31 July 2007 at 3:42am (Last edited: 31 July 2007 3:44am), Google Summer of Code Hacker, 76 Posts

As mentioned in [url=http://www.silverstripe.com/google-summer-of-code-forum/flat/2182?showPost=3084]this topic[/url] there's alternative way to parse RSS feeds by using the RestfulServices of mashups module.

For more information and updates on how to do it look into the wiki topic - [url=http://doc.silverstripe.com/doku.php?id=mashups#how_to_use_mashups_to_easily_embed_an_rss_feed]http://doc.silverstripe.com/doku.php?id=mashups#how_to_use_mashups_to_easily_embed_an_rss_feed[/url]

Go to Top