Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » RSS through RestfulService parse error

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: 1116 Views
  • klikhier
    Avatar
    Community Member
    150 Posts

    RSS through RestfulService parse error Link to this post

    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!

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: RSS through RestfulService parse error Link to this post

    Exception handling?

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

  • klikhier
    Avatar
    Community Member
    150 Posts

    Re: RSS through RestfulService parse error Link to this post

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

  • klikhier
    Avatar
    Community Member
    150 Posts

    Re: RSS through RestfulService parse error Link to this post

    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 http://www.cnbc.com/id/19811193/device/rss/rss.xml

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