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.

General Questions

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

RSS through RestfulService parse error


Reply

4 Posts   1128 Views

Avatar
klikhier

17 February 2010 at 10:23am (Last edited: 17 February 2010 10:26am), Community Member, 150 Posts

Hi there,

I'm using this piece of code to embed news to a site via RSS.

   // Accepts an RSS feed URL and outputs a list of links from it
   function RestfulLinks($limit) {
      $delicious = new RestfulService('http://www.cnbc.com/id/19811193/device/rss/rss.xml');      
      $conn = $delicious->connect();
      $result = $delicious->getValues($conn, "channel", "item")->getRange(0,$limit);
      $output = '';
      foreach ($result as $key => $value) {
         $output = '<b>' . $value->pubDate . '</b>' . ' &ndash; ' . $description . ' <a class="thickbox" href="'.$value->link.'?KeepThis=true&TB_iframe=true&height=600&width=960">More...</a>';
      }
      return $output;
}

This works perfectly, but sometimes I get the following error (and website is unavailable!):

Error: Uncaught Exception: String could not be parsed as XML
At line 217 in ......./public_html/sapphire/api/RestfulService.php

It looks like temporarily there is something strange going on in the feed (in this case wrong formatted or unavailable RSS feed?). Is there a way to make sure that this error doesn't affect my complete site. Something like: if feed cannot be parsed: do nothing.

Help is more than welcome, thanks in advance!

Avatar
Hamish

18 February 2010 at 7:40am (Last edited: 18 February 2010 7:40am), Community Member, 712 Posts

Exception handling?

try {
   // Code that might fail
} catch (Exception $e) {
   // Stuff to do if it fails.
}

Avatar
klikhier

18 February 2010 at 8:46am Community Member, 150 Posts

I'm sorry, I started with SilverStripe as a designer... learning by the day. Thanks Hamish, I managed to get that implemented.

Avatar
klikhier

18 February 2010 at 8:54am Community Member, 150 Posts

One question remains. As this worked before (parse errors started popping up a couple of days ago and I haven't been working on/updating this particular site), is there something wrong with this RSS feed that may be causing the parse error. Does anyone has an idea?

The feed URL is [url]http://www.cnbc.com/id/19811193/device/rss/rss.xml[/url]