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

Limiting RSS output results


Go to End


2 Posts   1347 Views

Avatar
kevino

Community Member, 30 Posts

24 October 2009 at 1:30am

Edited: 24/10/2009 1:32am

I've set up a RestfulService function to output an RSS feed, which works fine.

It's pretty much as described in the documentation here:
http://doc.silverstripe.org/doku.php?id=restfulservice

I've tried to modify the code myself to limit the number of results displayed to 5 instead of the 20 or so which are in the incoming feed.

My code looks like this currently:


	// Accepts an RSS feed URL and outputs a list of links from it
	function RestfulLinks($url){
		$feed = new RestfulService($url);
    $feedXML = $feed->request()->getBody();
    $result = $feed->getValues($feedXML, 'channel', 'item');
    $output = '';
		foreach ($result as $key => $value) {
			// Fix quote encoding
			$description = str_replace('"', '"', $value->description);
			$output .=  '<li><a href="'.$value->link.'">'.$value->title.'</a><br />'.$description.'</li>';
		}
		return $output ->getRange(0,5);
	}

I get the error:

Fatal error: Call to a member function getRange() on a non-object in /mysite/code/Page.php on line 87
(line 87 is the "return $output ->getRange(0,5);")

Can anyone suggest which direction I should be going?

Thanks!

Avatar
dalesaurus

Community Member, 283 Posts

24 October 2009 at 10:02am

$output is a php primitive string type, no such function exists (a string is not an object).

Remove that ->getRange() call, or use an appropriate method for a string if you need to do some kind of formatting. If you only want to first 5 of those use a for($i=0; $i<5;$i++){} loop instead of foreach