We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6513
    • 28 Posts
    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 sad

    Thanx,
    Daniel
      • 8609
      • 607 Posts
      I thought the code looked familiar: http://codingpad.maryspad.com/2010/01/15/modx-custom-snippet-api/ wink

      This snippet as I created it was meant to be very simple, pulling in only one feed per snippet call. If you want to pull in more than one feed in one snippet call, you have two options:

      1. Use something like feedtwister or something like that to combine your feeds and then put the resultant feed into your call

      2. Use SimplePie, it’s not that difficult to incorporate it into a snippet. I’m actually building something like that for one of my projects, might make a tut out of it.

      Check the repo too and make sure there isn’t already something that fits the bill. But if you want to roll your own, consider one of the two options above
        • 6513
        • 28 Posts
        Thanx!
        I actually have a DB table with a list of feeds that I would like ot display randomly on a few pages - eventually smiley

        I will look into simple pie and see what I can come up with smiley

        I’ll post my resultant code when I get it finished - it will probably be messy though...

        Thanks again,
        Daniel
          • 6513
          • 28 Posts
          I found a simplepie extension for Modx, but I get a strange 404 file not found error when I try to save the snippet.

          This is the actual error message "You don’t have permission to access /directorysexy/manager/index.php on this server."
          This is the folder where my Modx installation is located. I cannot figure out why I get this error????? The install is actually on a subdomain - projects.danielkraus-appserver.com/directorysexy/, where projects. is danielkraus-appserver.com/projects/.

          Could this be the reason for my error?? If so, how can I fix it? Or can I fix it?

          Thanx,
          Daniel