Jump to:

23453 Posts in 18902 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » External RSS

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

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

Page: 1
Go to End
Author Topic: 1895 Views
  • Mo
    Avatar
    Community Member
    508 Posts

    External RSS Link to this post

    Hi All,

    Quick question, I want to add the most recent news story from a third party RSS feed to one of my templates. I am quite happpy to do this via SimpleXML, but I was wondering if SS has something builtin that might be more preferable to use?

    Cheers,

    Mo

  • socks
    Avatar
    Community Member
    190 Posts

    Re: External RSS Link to this post

    I just grabbed code from the Blog module (RSSWidget.php).

    Attached Files
  • banal
    Avatar
    Community Member
    901 Posts

    Re: External RSS Link to this post

    Refactoring RSSWidget.php is probably a good solution. If you need more functionality, I'd probably use SimplePie (http://simplepie.org/) instead of parsing RAW XML using SimpleXML. It's a pretty neat php class for all your RSS/Atom Feed needs ;)

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: External RSS Link to this post

    Quick question. Has anyone tried to pull in the Silverstripe blog (http://www.silverstripe.org/blog/rss/)?

    I have tried several different methods, they all result in a file not found error, or something similar?

    Anyone know why?

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: External RSS Link to this post

    How are you doing it?

    We pull the ss.org blog feed fine from http://www.silverstripe.com/blog/.

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: External RSS Link to this post

    I got it working now, using recycled code from the blog module, as mentioned above:

    function LatestSSNews() {
          $output = new DataObjectSet();
          
          include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/SimplePie.php'));
          
          $t1 = microtime(true);
          $feed = new SimplePie('http://www.silverstripe.org/blog/rss', TEMP_FOLDER);
          $feed->init();
          if($items = $feed->get_items(0, 2)) {
             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;
          }
       }

    This works ok, I think the way I was trying to do it before was incorrect, I was trying to load it in using the RestfulService class, which didn't seem to work :s.

    Mo

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