MagpieRSS: RSS Simplified

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

  1. 1 Simran on Mar 16, 2006 at 2:56 am (Quote):

    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?

  2. 2 cavemonkey50 on Mar 16, 2006 at 6:47 am (Quote):

    Sure, I can try to help.

  3. 3 Simran on Mar 16, 2006 at 7:32 am (Quote):

    So, I’ve gone to the website, then clicked the download link which took me their sourceforge page. Which version should I download?

  4. 4 cavemonkey50 on Mar 16, 2006 at 2:43 pm (Quote):

    The latest, .72.

  5. 5 Simran on Mar 17, 2006 at 12:59 pm (Quote):

    Got it… now what? I’m really bad at this….

  6. 6 cavemonkey50 on Mar 17, 2006 at 4:36 pm (Quote):

    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.

  7. 7 Simran on Mar 18, 2006 at 4:01 am (Quote):

    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!

  8. 8 cavemonkey50 on Mar 18, 2006 at 11:54 am (Quote):

    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.

  9. 9 Simran on Mar 18, 2006 at 12:55 pm (Quote):

    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.

  10. 10 cavemonkey50 on Mar 18, 2006 at 1:08 pm (Quote):

    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.

  11. 11 Simran on Mar 19, 2006 at 12:45 am (Quote):

    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?

  12. 12 cavemonkey50 on Mar 19, 2006 at 9:47 pm (Quote):

    Simran: 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.

  13. 13 Simran on Mar 20, 2006 at 2:46 am (Quote):

    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.

  14. 14 cavemonkey50 on Mar 20, 2006 at 11:15 pm (Quote):

    How about this program?

  15. 15 Simran on Mar 20, 2006 at 11:57 pm (Quote):

    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?

  16. 16 cavemonkey50 on Mar 21, 2006 at 12:13 am (Quote):

    Here’s one for Windows that costs $10.

  17. 17 Simran on Mar 21, 2006 at 12:30 am (Quote):

    Aaaaa! Maybe that OSx86 install would be better then? haha… let me have a look at this.

  18. 18 Simran on Mar 21, 2006 at 12:46 am (Quote):

    it’s free…. just downloaded and nstalled it, working on configuring it now.

  19. 19 Simran on Mar 21, 2006 at 1:01 am (Quote):

    ok, its not working… damn! I did everything he said. for some reason, the xml isn’t appearing on my server.

  20. 20 chris on Mar 28, 2006 at 12:54 pm (Quote):

    HI.
    Can you provide a snippet showing how to get latest item and if has enclosures, get latest enclosure url?

  21. 21 cavemonkey50 on Mar 28, 2006 at 3:07 pm (Quote):

    Sure chris.

    require('rss_fetch.inc');
    
    $url = ""; // place the feed url in the quotes
    $rss = fetch_rss($url);
    
    $items = array_slice($rss->items, 0, 1); // limits rss output to one entry only (latest)
    
    foreach ($items as $item) {
    	$title = $item['title'];
    	$enclosure = $item['enclosure'];
    }

    That should do it. From there you can do what you want with the $title and $enclosure variables.

  22. 22 chris on Mar 30, 2006 at 12:36 pm (Quote):

    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?

  23. 23 cavemonkey50 on Mar 30, 2006 at 3:11 pm (Quote):

    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.

  24. 24 chris on Mar 30, 2006 at 4:00 pm (Quote):

    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.

  25. 25 cavemonkey50 on Mar 30, 2006 at 4:17 pm (Quote):

    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.

  26. 26 chris on Mar 30, 2006 at 4:30 pm (Quote):

    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 ;)

  27. 27 cavemonkey50 on Mar 30, 2006 at 4:35 pm (Quote):

    No, its not working for me either. I’m thinking maybe the patch will only work with an older version of MagpieRSS.

  28. 28 chris on Mar 30, 2006 at 4:48 pm (Quote):

    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.

  29. 29 Ryan Parman on Jun 24, 2006 at 3:38 pm (Quote):

    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!

  30. 30 cavemonkey50 on Jun 24, 2006 at 3:47 pm (Quote):

    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.

  31. 31 shakti on Jun 28, 2006 at 5:13 am (Quote):

    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…

  32. 32 Adam on Nov 13, 2006 at 4:52 pm (Quote):

    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.

  33. 33 Adam on Nov 13, 2006 at 4:54 pm (Quote):

    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;
    }

  34. 34 Adam on Nov 13, 2006 at 5:04 pm (Quote):

    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.

  35. 35 Bruce on Dec 3, 2006 at 3:37 pm (Quote):

    The above is terrific thank you.

    I am stumped on one though.
    I can’t get this format to show:
    for the chanel image

  36. 36 Bruce on Dec 3, 2006 at 3:38 pm (Quote):

    sorry:
    <itunes:image href=

  37. 37 Kristyn_K on Jan 15, 2007 at 1:33 pm (Quote):

    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

  1. [...] MagpieRSS: RSS Simplified [...]

Post a Comment

If you have the urge to code, run it through Postable and <pre><code>wrap it up</code></pre>.