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.
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:
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.
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:
Here’s another library that is included from Evo:
Maybe this in Revo could be used for that. Someone with some free time could check that out:
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.
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.