With the recent trends in websites, RSS is becoming a vital components of just about any web application. Long gone are days of RSS being for news only. Since so much data is being put out in RSS feeds, they can be very useful to a modern web developer. Just this past week I wrote two web applications that have used RSS as the main data source.
Now unless you have all time in the world, parsing RSS can be a job in itself. Luckily I didn’t have to do the parsing myself and neither will you. There is a free PHP RSS parser out there named MagpieRSS. MagpieRSS handles all the RSS parsing for you, and only returns the data you need. On top of that, MagpieRSS also caches the feed data, saving you time writing a caching mechanism.
Using MagpieRSS is a breeze. You only need two lines of code to get started; an include and a function.
require('rss_fetch.inc');
$rss = fetch_rss($url);
Once you’ve parsed the feed you can pull any value out you want. All you need is a simple foreach statement to get you started.
// Pull the RSS channel title from the feed
$title = $rss->channel['title'];
// Return all the URLs of the items
foreach ($rss->items as $item) {
$url = $item[link];
}
As you may have guessed, I absolutely love MagpieRSS. I will be using it in just about any project from now on that requires the use of an RSS feed. If you’re interested in using MagpieRSS for your next web project, I highly recommend checking out their website. There are just so many possibilities for this simple function.

37 Comments
I tried and tried and tried to configure, but I just can’t manage.
Basically I have this RSS feed, my last played songs from last.fm and I want to display them as JSS on my blog. Think you can give me a hand?
Sure, I can try to help.
So, I’ve gone to the website, then clicked the download link which took me their sourceforge page. Which version should I download?
The latest, .72.
Got it… now what? I’m really bad at this….
Now before I give you the lengthy explanation, you do know there are a number of WordPress plugins that do just what you want? Scrobbler happens to be my favorite and is very easy to install. Using a plugin will be a lot easier than writing the code yourself.
OK, I went and installed that plug-in. It’s completely useless (no offence), it only dislays the overall last.fm songs/albums, not my own! I want to display my last.fm feed!
Simran, you actually have to edit scrobbler-config.php for your feed to be customized. Now there is another plugin, WP Audioscrobbler, which isn’t as fancy as Scrobbler, but it has all its configuration settings within WordPress’ interface.
hey! it works…at least i think it does. ive installed it. the offline message is showing. im gonna listen to a whole bunch of music and see if it shows. thanks for your help. i want to learn the actual coding, but at some later point, right now im still trying to master html and css (yes I’m lame… hehe).
listen, i have one more question. i heard there was a way to have the xml file of your itunes library automatically sync with a server (possibly my own) every few minutes and then display the last plays from that on a website (my blog). there was a guy who had some itunes plugin that made this easy, but i dont know of it anymore or how in the world to use it. i think last.fm does something similar, by taking the playback names of songs in your itunes library to play more songs that you like/listen to.
As far as I know, the only thing you can do with iTunes’ XML file is to run it through a program and create a HTML file. That HTML file can then be uploaded to your website to view all the tracks you have in your library. The program I use for this is called iTunes XHTML Playlist.
i checked it out. it doesnt have live updating though? i mean, everytime i want to show the new songs ive listened to, ill have t carry out the process all over again?
Correct. The program is only meant to display what’s in your library and every time you add songs you have to reupdate it.
so, do you know of a way or program do have the last plays from itunes upload to some xml file on a server and then we can actually use magpie to convert that xml. we just need to find a way to have it updating.
How about this program?
wow! it’s super! im on windows though!!!
Sorry for the trouble, but do you think theres a similar one for xp or should i just finally go about an osx86 install?
Here’s one for Windows that costs $10.
Aaaaa! Maybe that OSx86 install would be better then? haha… let me have a look at this.
it’s free…. just downloaded and nstalled it, working on configuring it now.
ok, its not working… damn! I did everything he said. for some reason, the xml isn’t appearing on my server.
HI.
Can you provide a snippet showing how to get latest item and if has enclosures, get latest enclosure url?
Sure chris.
That should do it. From there you can do what you want with the $title and $enclosure variables.
HMM. Thank You but this will not work for me.
It seems to just print “(array)”.
I am using the proper rss_fetch too.
Does it work for you?
Well it won’t work in this form. I just gave you the framework to work from there. From what it sounds like, you want to make a link. In that case add the following line in the foreach:
echo '$title‘;Now I haven’t tested this specific code snippet yet, but looking over it, everything seems in order. If it doesn’t work after this I’ll try and see what’s wrong.
Yeah I know how to use variables.
Just for some reason when I use $enclosure in this case, it is not printing the url of the enclosure.
Let me know if it works for you so I know if it’s my end or not.
Maybe my patched rss_fetch needs another look, but it does contain the hack to put enclosures in an array.
The only thing I can think of is you need to go one level deeper on the enclosure variable. Try replacing the enclosure variable with this:
$enclosure = $item['enclosure']['url'];If you have more details on the hack could your provide me with them? Without knowing what you changed I can’t be of much assistance.
Right.
I actually tried that to.
As far as I know, magpieRSS does not natively support enclosures.
Their was a patch to make it work.
The instructions actually say to do what you are suggesting.
I dont know
see here -
http://rss.gregarius.net/MagpieRSS___PHP_RSS_Parser/2005/01/15/
http://laughingmeme.org/code/rss_parse.inc.with.enclosures
If you try it and it works, I will know that I need to refresh the magpie code as I may have messed it up a while back.
Thanks for your help anyways
No, its not working for me either. I’m thinking maybe the patch will only work with an older version of MagpieRSS.
Thanks.
I can’t believe the apparent best RSS parser cannot do enclosures!
I will have to spend time to fix this I guess.
Thanks for confirming.
I can’t help but to plug SimplePie (http://simplepie.org). A list of major features can be found on the homepage. SimplePie aims to make it ridiculously simple to get feeds into your pages. It supports 98 character encodings seamlessly, support for enclosures (as well as automatically embedding them), caching, one-click subscriptions and one-click bookmarks, and Beta 3 (due out next weekend-ish) will also support HTTP Conditional Get, which is simply a much more efficient way to manage cache.
If you’re looking for something that we believe it better than Magpie RSS, I’d ask that you check out SimplePie.
Oh, and total off-topic. I’ve liked some of your Mint plugins!
Thanks for making me aware of this Ryan. I was having localization issues with MagpieRSS and Blog Comments, so maybe SimplePie will solve that issue.
hi,
i’m new at RSS. no idea how to build a web with RSS application. do you know where i can find a complete PHP source code (an example to build this application) ? thanks…
For whatever reason when It copied his entire script I got an error on line 148 for: ” $attrs = array_change_key_case($attrs, CASE_LOWER);” - so I just isolated the mod and tried adding it.
I got chris’s mod to work by taking just the piece of code which is the mod and adding it to my parse_rss include.
goto the link he posted: http://laughingmeme.org/code/rss_parse.inc.with.enclosures
and ctrl+f “enclosure” (second one)
then copy the elseif statement which contains the ‘enclsoure’ you found.
and paste it on line 208 of your include right after the end of the previous elseif and a line above the “# handle atom content constructs” comment. That’s about it.
Thanks Chirs. I might try simplePie some other time - I already have my scripts built for Magpie so maybe next site.
incase his site goes down, here is the mod for .7a
line 208 (or an unmodified .7a rss_parse.inc):
elseif ( $this->feed_type == RSS and $el == ‘enclosure’ )
{
$this->current_item[$el][] = $attrs;
$this->incontent = $el;
}
ok last post.
With the mod I posted, you’ll find the actual values inside the enclosure inside of :
$enclosure = $arr_Item['enclosure'][0];
So it’d be like:
$str_PodcastUrl = $arr_Item['enclosure'][0]['url'];
$str_PodcastLength = $arr_Item['enclosure'][0]['length'];
$str_PodcastType = $arr_Item['enclosure'][0]['type'];
kind of obvious but just incase any noobs got confused abou it. Hell, here have my debug code noobs:
// Change to your rss feed
$str_URLFeed = “./yourFeed.rss”;
$obj_RSS = fetch_rss($str_URLFeed);
foreach ($obj_RSS->items as $arr_Item)
{
echo “====DEBUG====”;
foreach ($arr_Item as $key => $value)
{
echo “key: “. $key .”\tvalue: “.$value;
if($key == ‘enclosure’) {
foreach ($value[0] as $key => $value)
{
echo “\tkey: “. $key .” \ \t value: “.$value .”";
}
}
echo “===”;
}
}
// No garuntees it’ll work - just a copy paste.
The above is terrific thank you.
I am stumped on one though.
I can’t get this format to show:
for the chanel image
sorry:
<itunes:image href=
Hi, I’ve just installed Magpie and it works great! I want to know where in the relevantfeeds.php doyou insert the code
$num_items = 10;
$rss = fetch_rss($url);
$items = array_slice($rss->items, 0, $num_items);
foreach ( $items as $item ) {
in order to control the number of feeds displayed?
One Trackback/Pingback
[...] MagpieRSS: RSS Simplified [...]
Post a Comment