Has anyone ever made a RSS parser module? I would like to add the latest posts from team members blogs to our website.
:)
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Has anyone ever made a RSS parser module? I would like to add the latest posts from team members blogs to our website.
:)
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;
}
Or use Simplepie (http://www.simplepie.org) - it's a bit smarter/faster/up-to-date than Magpie.
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? :)
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.
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/
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 http://lastrss.oslab.net/lastRSS.phps 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('&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 http://new.elijahlofgren.com/ 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 Re: Zend Framework's implementation of Mashup Web Services
As mentioned in this topic 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