Hello all,
I have put together an RSS feed snippet but my php is not that great. Currently I am calling 2 different snippets for the feeds. One for the title of the feed and one for the feed items. I would like to have it as a single snippet but have failed at doing so on my own.
snippet for feed title:
<?php
//default parameter values
//create a variable to hold the output
$output = '';
//retrieve file and return as string
$content = file_get_contents($feed_url);
//all is good, we parse the feed
$feedtitle = new SimpleXMLElement($content);
//iterate over item in the channel and get the title of each item
$modx->setPlaceholder("fffeedtitle", $feedtitle->channel->title);
$modx->setPlaceholder("fffeedlink", $feetitle->channel->link);
$output .= (!isset($tplChunk)) ? $modx->parseChunk('rssfeedTitletpl', $modx->placeholders, '[+', '+]') : $modx->parseChunk($tplChunk, $modx->placeholders, '[+', '+]');
//set up a counter to determine how many items to be displayed
return $output;
?>
snippet for getting feed items:
<?php
//default parameter values
$limit = (isset($limit)) ? $limit : 10;
$cssChunk = (isset($cssChunk)) ? $cssChunk : 'rssfeedFetcherCSS';
//Inject the CSS code into the head section
$modx->regClientCSS($modx->getChunk($cssChunk));
if (!function_exists(fetchFeed)) {
function fetchFeed($feed_url, $limit, $cssChunk, $tplChunk){
global $modx;
//create a variable to hold the output
$output = '';
//retrieve file and return as string
$content = file_get_contents($feed_url);
try {
//all is good, we parse the feed
$feeditems = new SimpleXMLElement($content);
//iterate over item in the channel and get the title of each item
foreach($feeditems->channel->item as $entry){
//set up a counter to determine how many items to be displayed
if ($i < $limit) {
$modx->setPlaceholder("fftitle", $entry->title);
$modx->setPlaceholder("fflink", $entry->link);
$output .= (!isset($tplChunk)) ? $modx->parseChunk('rssfeedFetchertpl', $modx->placeholders, '[+', '+]') : $modx->parseChunk($tplChunk, $modx->placeholders, '[+', '+]');
}
$i++;
}
} catch (Exception $e) {
//some error occured, we output an error message and a description of the error
$output .= 'An error occurred. The feed ' . $feed_url . ' could not be read: ' . $e->getMessage();
}
return $output;
}
}
//call the function
return fetchFeed($feed_url, $limit, $cssChunk, $tplChunk);
?>
I found this code snippet on a tutorial for Modx Snippets cannot remember the url though
Thanx,
Daniel