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.

We've moved the forum!

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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Displaying external RSS feed on page


Go to End


4 Posts   2461 Views

Avatar
ramshackle

Community Member, 22 Posts

15 February 2011 at 8:43pm

Hi there,

I am trying to display one external RSS feed on a site homepage. I have looked around a fair bit and thread http://silverstripe.org/customising-the-cms/show/11548 is a convoluted way of doing it, with formatting problems and the "=" in links in the feed text become "%" when you click through so they don't work.

So is there an easier way of doing this? I just want to display one external RSS feed in a small window on the home page.

Cheers

Avatar
swaiba

Forum Moderator, 1899 Posts

15 February 2011 at 10:47pm

I am sure there is a better way, but I've used simplepie for this in the past...


<?php
class RSSJobsPage extends Page {}
class RSSJobsPage_Controller extends Page_Controller {
	function RSSJobsPage(){
		$output = new DataObjectSet();
		$feed = new SimplePie('http://www.silverstripe.org/blog/rss', TEMP_FOLDER);
		$feed->init();
		if($items = $feed->get_items(0, 10)){
			foreach($items as $item){
				// Cast the Date
				$date = new Date('Date');
				$date->setValue($item->get_date());

				// Cast the Title
				$title = new Text('Title');
				$title->setValue($item->get_title());

				// Cast the description and strip
				$desc = new Text('Description');
				$desc->setValue(strip_tags($item->get_description()));
				$output->push(new ArrayData(array(
					'Title'         => $title,
					'Date'         => $date,
					'Link'         => $item->get_link(),
					'Description'   => $desc
				)));
			}
		}

		return $output;
	}
}

attached is the source code for simple pie....

Attached Files
Avatar
ramshackle

Community Member, 22 Posts

1 March 2011 at 2:41pm

Thanks Swaiba, sorry for the delay.

I'm pretty new to php and the SS setup I'm afraid - would I put this code as a php file in mysite/code? I'd like the external feed to appear on the homepage, so it would be called in that template - how would I call the feed from the homepage.ss template?

Also where would I need to put the SimplePie php file?

Cheers

Avatar
edski

Community Member, 12 Posts

10 December 2011 at 3:26am

I'm not sure the advantage of using the SimplePie.php file. I found that if I add in the following line to the code example it works:

include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/simplepie.inc'));

<?php
class RSSJobsPage extends Page {}
class RSSJobsPage_Controller extends Page_Controller {
   function RSSJobsPage(){

      include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/simplepie.inc'));

      $output = new DataObjectSet();
      $feed = new SimplePie('http://www.silverstripe.org/blog/rss', TEMP_FOLDER);
      $feed->init();
      if($items = $feed->get_items(0, 10)){
         foreach($items as $item){
            // Cast the Date
            $date = new Date('Date');
            $date->setValue($item->get_date());

            // Cast the Title
            $title = new Text('Title');
            $title->setValue($item->get_title());

            // Cast the description and strip
            $desc = new Text('Description');
            $desc->setValue(strip_tags($item->get_description()));
            $output->push(new ArrayData(array(
               'Title' => $title,
               'Date' => $date,
               'Link' => $item->get_link(),
               'Description' => $desc
            )));
         }
      }

      return $output;
   }
}