We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21560
    • 145 Posts
    This is a very simplified snippet to read RSS feeds from another site and display them on yours.

    Why would this be useful? Well, in the case of the company I work for, we have a blog that we want to show titles and content for.

    This should be an interesting step up for someone who’d like to make a serious snippet out of this.

    Example call:
    [!RSSFeed? &url=`http://nimja.com/nimja.rss` &words=`30`!]

    <?php
    // RSSFeed reader by Nimja.com
    // Save as "RSSFeed"
    
    //URL to read, can be set in snippet call.
    $url = (!empty($url)) ? $url : "http://nimja.com/nimja.rss";
    
    //Words in the 'preview', can be set in snippet call.
    $words = (!empty($words)) ? $words : 30;
    
    //Get feed contents (the easy way)
    $feed = file_get_contents($url);
    
    //Open a dom object and get all the 'item's
    $dom = domxml_open_mem($feed) or die('error');
    $root = $dom->document_element();
    $items = $root->get_elements_by_tagname('item');
    
    //Loop over the items.
    foreach ($items as $item) {
    	$title = $item->get_elements_by_tagname('title');
    	$desc = $item->get_elements_by_tagname('description');
    	$link = $item->get_elements_by_tagname('link');
    	$date = $item->get_elements_by_tagname('pubDate');
    	
    	$time = strtotime($date[0]->get_content());
    	
    	$text = strip_tags($desc[0]->get_content());
    	$words_array = array_slice(split(' ', $text), 0, $words);
    	$description = implode(' ', $words_array).'...';
    	
    	echo '<h2>'.$title[0]->get_content().'<span>'.date('d M Y',$time).'</span></h2>';
    	echo $description.'<br />';
    	echo '<a href="'.$link[0]->get_content().'">Read More</a><br /><br />';
    }
    ?>


    ps. For the less lazy, it should be fairly easy to create an ATOM reader the same way. I didn’t look at the differences myself much.
      [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
      • 25483
      • 741 Posts
      Nice one, I think I can use this on one of my sites smiley
        with regards,

        Ronald Lokers
        'Front-end developer' @ h2o Media

        • 21560
        • 145 Posts
        Oh, and just to show it in action:

        http://www.datagenicgroup.com/resources/blogs-at-datagenic.html

        The whole site is MODx, obviously. wink

        And I kind of hoped people would find it useful. I couldn’t find a snippet like this in the repository, so I made it.
          [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
          • 25483
          • 741 Posts
          Quote from: Nimja at Feb 03, 2009, 08:33 AM

          Oh, and just to show it in action:

          http://www.datagenicgroup.com/resources/blogs-at-datagenic.html

          The whole site is MODx, obviously.  wink

          And I kind of hoped people would find it useful. I couldn’t find a snippet like this in the repository, so I made it.

          there is a bigger snippet on the forum called feedx (http://modxcms.com/forums/index.php/topic,12657.0.html), that one has more powers (templating and stuff). But I like the simplicity of your snippet smiley
            with regards,

            Ronald Lokers
            'Front-end developer' @ h2o Media

            • 36592
            • 970 Posts
              • 7231
              • 4,205 Posts
              But I like the simplicity of your snippet grin
              Me too, both PieX and FeedX are both very powerfull but also can be somewhat complicated to do a simple feed read. Thanks for sharing wink
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don&#39;t know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 21560
                • 145 Posts
                *smiles* Well, I share these things also because it might offer some people interesting bits of PHP coding that will be used.

                I wasn’t aware of FeedX and PieX, I might have used them instead but it didn’t take me that long to make anyway. ^_^
                  [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
                  • 10449
                  • 956 Posts
                  Just a sidenote:

                  $feed = file_get_contents($url);


                  You can’t use certain characters in snippet parameters. If a feed URL includes any of these: = ? &
                  e.g. http://www.unog.ch/80256EDD006AC19C/httpRSSFeed?ReadForm&count=10000&lang=en
                  The URL will be cut off. The two a.m. snippets take that into account, afaik. You can use replacement strings like |eq| to avoid this problem.

                    • 21560
                    • 145 Posts
                    I am aware of this. But as I said, this is just a simple snippet to build upon. Ideally you’d make a small strtr function with some replacements of characters that don’t work in snippets.

                    Besides, if I recall correctly, it will be fixed soon-ish?

                    For those less savy:
                    $replace = [
                    	'QM' => '?',
                    	'AMP' => '&'
                    ];
                    $url = strtr($url, $replace);
                    

                    Usage: QM and AMP will be replaced by a ? and & respectively in the snippet call. Place this code after the $url = (!empty.... line
                      [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com