Showing RSS feeds from other sites is a handy way to tie different media sources together. In the small business sites that I’m making lately, everyone wants to have a Twitter and a Facebook link. The next level would be to display RSS feeds from elsewhere right on your site instead of only having a link leading away from your site. It centralizes things a bit more, and may lead to someone spending more time on your site.
WordPress, blog extraordinaire that it is, already has loads of plugins for that which will get you up and running in minutes. MODx also has some RSS feed code here and there, but nothing quite that quick to implement. This week I experimented with a couple ways to display feeds, using code contributed by MODx users. There are other ways, too.
Although I can use and modify PHP code, generally programming is not my favorite task. What I really like to code is CSS, and I am a generalist rather than a programming specialist. So you could say that this is from the point of view of a part-time PHP coder, not a hardcore programming geek, and I hope it may help others who do not enjoy pondering polymorphism, encapsulation, and all that other fun stuff.
Feedreader
First, there is a MODx snippet (essentially a small PHP script) called Feedreader, written by a Brazilian fellow. This one was very easy to use and worked very well. I did my test with Evo, which is MODx version 1. You simply give it the feed URL in your call. It reads the feed, grabs the title, description, link, and date, and displays them. Muito obrigado, so nice and easy!
Your basic Evo call in your page or template would look something like this (don’t forget backticks around the URL):
[[feedreader? &urlrss=`http://myfeedsite.com/feed.rss` &title=`My Clever Title`]]
I made a couple little changes to it, I added an input parm that lets you control the number of items displayed. Using a parm called “number”, this code defaults to displaying 5 items, but allows you to have more or less if you want. I added this at line 28:
$number=(isset($number)) ? $number : 5;
While testing that, I noted that one of his loops was off by one. My code, at line 51, starts the loop from 1 instead of zero, and then it matched up exactly with the parm:
for($i=1;$i<=$tamanho;$i++){
You may note that there’s a “foreign” word in there. ["Oh, no!" said the fearful monolingual user.] I do know a little Portuguese (it means “size”, and is almost identical to the Spanish word), but even if I didn’t, this script is very easy to follow, so don’t you worry. It just reads the feed, gets certain fields, does some UTF-8 decoding, and echoes the fields to the screen. Bing bing bada bing! I’m confident that it would work in Revo, it’s so dirt-simple and self-contained.
There’s also a spot in the code that hardcodes HTML TITLE tags for the RSS title. You definitely don’t want a title tag there, that’s for the top of your page, so I just changed it to a header tag to taste. Not a huge deal.
RSS Import
I also tried RSS Import, which hails from Russia. Even so, all notes are in English, so fear not, non-Cyrillic alphabet readers. This one gets a bit fancier. It handles caching, though I don’t know the details. It uses the popular Snoopy class that is used for pulling content from another site and sticking it in your page. It also examines the feed, and if it’s not UTF-8, it converts to that. It also offers a host of parms for date formatting, number of items, and MOST importantly, you can template the feed using a MODx chunk if desired!
Example call from a page or template:
[[RSS? &url=`http://rss-url` &num=`5` &tpl=`rss-output`]]
A Twitter feed sailed right through, nice and clean, and my template worked great. Then I tried a Youtube feed and ended up getting raw HTML instead of nice output. I did some digging and found that Youtube uses entities for two key HTML symbols, the brackets that enclose statements. I used the following two statements to convert these back to normal, and boom, everything was fine again. These lines went around line 62 or so:
$source = str_replace("<", "<", $source);
$source = str_replace(">", ">", $source);
While I was examining the Youtube feed, I noted that it spews out everything in HTML tables. ["Yuck!" said the CSS guy.] But it works, you’ll just bust your XHTML validation, if you are concerned about that. I even found a hack-ish way around that, and I’ll publish it if anyone is interested. I was grabbing a feed of uploads – other Youtube feeds may possibly be nice and clean, and without tables, but I didn’t get quite that far.
I also did a test with an Atom feed. Although the comments in the code seem to indicate that Atom feeds should work, mine did not. I found some points of difference in this feed that the code was not set up for, and if I want RSS Import to work with those, I’ll need to recode it. I’m not enamored of wading through text parsing code, but I may try it if I have time. For fun, I tried the same thing with Feedreader, and it also seems to be set up for regular RSS only.
I tried testing the cache function, and it caused an error trying to write a cache file. I didn’t have time to investigate, but it looked like a path was bad.
I’m not as sure if this snippet will work in Revo, as it relies on a couple of includes. But I think it should work fine. If you’re adapting it for Revo, you might want to make note of a couple file locations. Snoopy is apparently here in Revo:
core\model\modx\xmlrss\extlib\snoopy.class.php
Here’s another library that is included from Evo:
manager/media/rss/rss_cache.inc
Maybe this in Revo could be used for that. Someone with some free time could check that out:
core\model\modx\xmlrss\rsscache.class.php
So I hope that’s helpful. In the past I’ve tried some other feed snippets. I recall one that used SimplePie, but it seemed anything but simple, with many scripts using it not surviving SimplePie upgrades.
So I’d recommend feedreader for quick and easy results, RSS Import if you need something a little fancier.
As of 5/3/11 – I just tried getFeed for Revo. Pretty nice.
getFeed
This can be installed via Revo Package Management. Doc is here. I did a quick test, and it worked really well with a Twitter feed. To get meaningful output, you have to template it, which brings up an interesting point. If you run the snippet without a template, you get an array of the feed contents. This is great, because then you get a good look at your data, and you can make note of fields you can use as placeholders in your template. Quite handy, moreso than looking at raw feed data. I tried running a couple other types of feeds through it and got no output. It may be expecting a certain format, and these other feeds may have been in atom format or the like. Maybe I’ll get a chance to hack it apart and find out details sometime soon.
hi, getFeed is not doin it for me. i have a page containing a chunk, and my chunk contains:
[[!getFeed? &url=`http://feedrinse.com/services/channel/?chanurl=fa609d96eb689ea757d5ae63925045f5`]]
Ah, Johnny Why, you already have a reputation for asking all those nettlesome, yet possibly useful, questions on the MODx forum.
I think you’d be happier with another system.
I also tried your URL in GetFeed, no dice. I tried it in Evo with those two other apps, and they didn’t work either. Looking at the code briefly, it may be the CDATA portions that bork it, but it would take more analysis to determine that.
As I view the raw code of more feeds, it has become clear that there are a variety of format variations, so to get a really robust feed reader, one must have very flexible parsing code.
Hi Dave, First off there is another fellow out there with your name who is the creative director of Tumi. Unless that is you! My question, in Modx Evo I am using Modx RSS feed. Everything seems to work well. I have made significant changes to a document/resource. I would like that to appear as a new item on the rss feed with the snippet. How do I get that document to appear as a new RSS item? What triggers a document to be picked up by the RSS feed with the snippet so that it might be reset? Thanks
Thomas, I’m not that guy, but I’ll take his paycheck if he doesn’t want it!
Anyway, if you installed Evo with the example content, you will have an RSS feed via Ditto. The URL to this document can become the URL parm for RSS Import above! I just tested it, and it works fine. If you have no example content, click here to see what is needed to make an RSS feed page. Note the arrows for important stuff.
Pay special attention to the “parents” parameter. I would suggest you have your intended RSS documents be in a particular folder, and use that folder as your Ditto “parents” parameter. If instead you have Ditto pointing at all documents, some will look funny, like Maxigallery pages.
Hope that helps! Dave
Thanks David, I think that maybe I just need to go one step further here and I did some further searching since reading you comments here. I do use the Ditto call you mention. Here it is: [[Ditto? &parents=`49, 50, 51, 52, 54, 55, 57, 48` &format=`rss` &display=`30` &dateSource=`pub_date` &orderBy=`pub_date DESC` &total=`30` &tpl=`rss_item`]] I use the &dateSource=`pub_date` to pick up the publish date which I do update, rather than use the “created date” which will not pick up the document as new. I have a document 96 in folder(container) 50. Document 96 is the information on a trade show. (It searches very well with the Search engines and has a good friendly URL which I want to stay the same.) Initially when published it appeared in the RSS feed just fine. After the show, this document changes significantly to content for the next show and I update the “publish date”. At this point it appears in an RSS feed list correctly but will not update in an RSS Reader. Maybe it is a technical issue, but that is how far I have come.
Thomas,
That’s different, you may only have a Ditto problem. You have a cached call there, these are notorious for not showing updates quickly, and you’re pulling data from a lot of folders. Next thing to try is non-cached, instead of double-brackets, note the exclamation points: [!Blah Blah!]
I am assuming that the pub_date order is working.
Cheers
David, thank you for reminding me about un-cached. Pub_date order works fine as long as it is used with @orderBy. Otherwise is did not produce the results correctly. It would otherwise still order by the created on field. The RSS reader seems to be the only challenge as it will not pick up a resource with an updated pub_date. It still feels that resource was published earlier. If I create a new RSS Reader the feed results are fine, but on an established RSS Reader the resource is considered published earlier even if I am using the pub_date. The only instance where I think it becomes an issue will be if someone is following me on an RSS reader and therefore would not see as a new post the significant changes. The RSS feed itself looks fine.
Thomas,
Have you uncached the RSS page as well as the call? If so, that may be the end of the line for my ideas without tearing apart your site. You could also clear the cache for the whole site and see if that does anything. If none of that works, maybe facing a server performance issue, or Ditto is choking on many docs.
Cheers, Dave
Dave, the RSS page has been uncached. I just think it has to be something in the RSS reader. The RSS page at the URL appears fine, updates fine. The RSS Reader just does not see updated resources as new.
Hi, I’m using RSS import,
Somehow this feed only loads the first feed, it used to be oke, so I think it has to do with some text into the feed like %
The feed:
http://feeds.feedburner.com/mraccountants
Is there a solution to this like str_replace?
Hi,
I did a test with your feed, and it actually worked fine for me, displaying 10 articles. So it’s hard to say what might need to change. I know that I had to examine the code pretty carefully when I was testing it. Maybe your version of the snippet is different? There are other possible variables, too. I have been testing with Evo, not Revo, so that may work differently.
If it is a character issue, str_replace may indeed be necessary. I did see a number of character entities in your feed, such as
‘and’, types of quotes, so you may need to analyze the feed for these and do str_replace with them. It’s kind of a pain, I know. Sorry not to have an easy answer.Dave
Hi Dave,
Thank u for ur time to test this.
I’m using Evo too, is it possible u can email ( mac_aronie[a]hotmail[dot]com ) your snippetcode to me? Whats ur snippet call, and is the page cached or not.
I use:
[[ RSS?
&url=`http://feeds.feedburner.com/mraccountants`
&tpl=`rss-tpl`
&num=`20`
&dateFormat=`%d/%m/%Y`
]]
Page Cached
Greetz Marc
Thanks Dave,
Your snippet worked like a charm
4r, Glad that helped!
It’s not fun to have a site suddenly stop working. Once you start looking at actual RSS data, you realize that it’s very complex. Text parsing is always tough, and it’s very hard to anticipate all characters that could come through a feed in multiple languages. It’s beyond me! I’m seeing that Evo support is waning, just as we’d expect, so we’ll have to use duct tape and wire to keep things going!
Have you a suggestion for me, which snippet/modul i could use to import a feed and post it as an resource? I want a “real” rss import. Great would be, if i could choose which feed-articles to import by the module-administration.
If you have a suggestion on which snippet to use to get started with a custom solution, i would be grateful!
appreciated, cheers..
urbandigital
Urban,
Interesting question. I’d have to interpret what you mean by “real” RSS import. It sounds as if you’d like something that would read a feed, and from that, generate a new MODx page? If that’s true, I guess this could be a way to “re-blog” something (and I hope you would still give credit to the original source). It’s not something I would want – to me it seems like taking the long way home. That’s a lot more PHP programming than coding a snippet that simply reads a feed and displays the latest results. If I were going to do that, I would just probably sign up with paper.li
So if that’s what you want, sorry, I don’t have a snippet suggestion – sounds like custom code would be needed. I’m guessing that MODx super guru Bob Ray would have good ideas on that. Or maybe we’ve just run into a language barrier.
Mach’s gut, Dave