Grow your CSS skills. Land your dream job.

#55: Adding RSS Content with SimplePie

The "Really Simple" part of RSS is sometimes misleading. What if you want to pull in content from an RSS feed and display it on your site? Doing that from scratch would be no-so-easy, but thankfully we have wonderful SimplePie to do the heavy lifting for us. In this screencast we build a really simple site that grabs content from multiple RSS feeds, smushes them together, and displays them using our own custom markup.

Links from Video:

Download Video File

Comments

  1. Permalink to comment#

    Cool Screen cast Safari 4.0 beat is awesome

  2. Matt
    Permalink to comment#

    The Safari 4.0 Beta is very nice. I think they use OpenGL on Windows (I think, not sure). Anyways, Simplepie has always been on my favorite list of well made extensions. It gives full editing without fully having to mess with the internals.

    Anyways, I’ll stop the rambling (poor at that) and get to what I really wanted to ask:
    Any chance of touching on Joomla template development? I know you’re giving away the development book, for which I know the publisher is just amazing for their books, but is there any chance that you could pique our interests with some example template design?

  3. Antti
    Permalink to comment#

    Thanks for the screencast, again! :)

    I agree with Matt; could you perhaps tacle Joomla! the same way you took down WordPress (creating a theme from a PS layout)?

  4. Thanks so much, I finally have the courage to digg my teeth into SP. I didn’t realize it was that easy o_O

  5. Permalink to comment#

    Great screen cast! I think that it is helpful to know that there is also a SimplePie plugin for WordPress. I think that you can download it from the SimplePie page. Simple Pie is rad and I used it on the bottom of my site (http://benjaminal.com) to just keep up with friends and family whose blogs we follow. The fun part of SimplePie is being able to style the feeds.

    Multiple Twitter feeds are cool too.

    • Permalink to comment#

      Unfortunately, I can’t watch the screencast from my office for some reason (some kind of proxy issue probably) but I’ve tried to use the wordpress plugin in the past and always had issues. Maybe it’s just me though.
      Is it easier to just code in simplepie yourself to a theme?

  6. This is what I did with Simple Pie http://sermons.beimmersed.com let me know if you want the code.

  7. Donna
    Permalink to comment#

    Thanks Chris! I’ve used magpierss in the past which worked fine for me but this is so much easier! Another great screencast!

  8. kyle
    Permalink to comment#

    Hey Chris,

    Am I the only one that has issues opening up these videos in itunes? I subscribe to the podcast there, and every so often it simply won’t play. Plays fine here.

    Running Leopard, and the latest version of itunes.

  9. Permalink to comment#

    I checked the code in the demo page, hadn’t seen the screencast yet, and I see the feeds are actually rendered as HTML.

    Can someone confirm this please?

    Thanks.

    • That’s the whole point =)

      It pulls in the feed and renders as HTML so you can style to your heart’s content.

    • Matt
      Permalink to comment#

      The entire point to glorified RSS =)

      On a side note, does the iPhone/iPod Touch version of Safari have an RSS reader like Safari does on the Mac side? (sleek, blue, easy to scroll through?)

    • Permalink to comment#

      110% agree. More SEO friendly pages with relevant content for the visitors. Awesome!

      Thanks Xris.

  10. Thomas
    Permalink to comment#

    Nice job on this screencast, Chris! You seemed really natural and relaxed. Great tutorial too, I’m working on a site that will be using this awesome plugin too, can’t wait to show it off! :D

  11. This is Ryan from the SImplePie project. Great screencast! Here are a few additional notes for those following along:

    1) No, loading simplepie.inc doesn’t have a big load on the in-browser code, as it all runs on the server. Also, a large portion of that file size is from code comments. Lastly, it’s important to remember that making things easy is hard. :)

    2) For a PopURLs clone using SimplePie, check out NewsBlocks (http://newsblocks.simplepie.org)

    3) For the IDN folder, if you load it and include the include inside it, SimplePie will support Internationalized Domain Names.

    4) Caching is enabled by default, and writes to ‘./cache’ unless you tell it to write someplace else, and defaults to 1 hour.

    5) $feed->handle_content_type() deals with the character encoding (e.g. UTF-8) for the page. You could also use PHP’s header() function or even the tag to set the output to UTF-8.

    6) Check out some details on cache folder permissions. http://simplepie.org/wiki/setup/setup#step_3

    7) We’ve spent tons of time writing documentation for the entire API. That being said, if something doesn’t quite work the way you expect (such as $item->get_author() which returns another object with sub-methods) read the docs, then hit us up on the support forums. :)

  12. Permalink to comment#

    Great screencast, Chris! I recently used SimplePie in another project, and wrote a tutorial for how to use it with Flickr that’s on NETTUTS+ (http://is.gd/kQ4C)

    Love your work :)

  13. Will
    Permalink to comment#

    Chris,

    Regarding your comment at about 12:30 into the video when you said “is that an array?” No.

    $feed is an object. When assigning or accessing values in an object you use the -> operator, in an array the operator is =>

    Also, if it were an array it would have been declared as $feed = array(); any time you see $variable = new function(); the keyword new is the clue that you’re creating an object. :)

    • Will
      Permalink to comment#

      Also regarding permissions…

      If you set the owner of the folder to the username that the web server runs as you can grant write permissions to the user, and leave the rest empty.

      So the permissions would be 600, or 644 (if you want it world readable). there’s no need to have execution access set because you’re not running any cgi scripts from the folder.

  14. Great screencast. I’m interested in your thoughts, as an rss reader and publisher: When does reproducing rss feeds infringe on copyrights? Do you protect your content? Do you obtain permission from authors before reproducing headlines, previews or full articles? Is it fair that some websites are creating revenues (web traffic) from work they didn’t create? Just some thoughts…

  15. Omar
    Permalink to comment#

    Do you have to create separate feed objects if you don’t want to smush them all together?

  16. thank you for this post. It helps a lot.

  17. Matthew V
    Permalink to comment#

    How do you diferantiate between 2 or more diffrent feed arrays on the same page for two or more diffrent markups, such as popurls?

    • David Jones
      Permalink to comment#

      By default SimplePie smushes together all the feeds into one group and sorts it by date, so the feed items are intermingled.

      Actually if you wanted to present 3 feeds seperately as on PopUrls you would be better off using 3 instances of SimplePie.

  18. Wow! You’ve done it again. Basically, your tutorials are shaping all the work I do. Thank you.

  19. Permalink to comment#

    This was very useful. Haven’t been on this website before but hey, let’s add it to my combined brandnew RSS-feed!

    Thanks and keep it going. The tutorial was very clear.

  20. DavidFM
    Permalink to comment#

    First time in 12 years working online that I’ve watched a screencast all the way through!

    Loving your work (and loving SimplePie).

    Cheers

  21. webninja
    Permalink to comment#

    Nice tutorial. My first time watching your screencast, would be watching more :)

    One thing though, white spaces above the DOCTYPE trips IE 6 to quirks mode so it might be better to have the tag start right after your first php block. …?><!DOCTYPE…

    Cheers!

    • I have a feeling this is an old wives tale. Not 100% sure though and haven’t run any tests, but there is conflicting stuff out there on if this “whitespace before DOCTYPE triggers quirks mode” thing.

  22. Permalink to comment#

    Nice work, Chris.

    A quick, non-web-related critique:
    Use Audacity or some other audio program (or whatever video software you might be using) to compress your audio so there’s isn’t such a dynamic shift in volume when you’re far away from your mic then get close up.

  23. Is it possible to setup SimplePie to have a website displaying multiples feeds, but :

    when you click on a feed “item” (permalink), instead of going to the item’s full article on the feed’s website, you dispaly the article on your own website, with your style (and of course you provide a link to the original source / item’s article on feed’s website).

    Kind of, instead of that:
    list of feeds > list of items > original article on feed website.

    That:
    list of feeds > list of items > content > (link to original article on feed website).

    Thanks in advance for enlighting me.

    • Permalink to comment#

      Gian, I hacked it for google news. I was able to do it because google news has a set format for the URL and the same number of characters from the beginning of the feed string until the point where the source URL appears. User the ‘substr’ command in PHP to slice off the portion of the URL string that contains the source URL.

  24. is there a way to set the number of items that are displayed?

    • i know there’s set_item_limit, but that only limits the items from each feed. what if you have a “feedsmusher” and want to limit the number of items that smusher is displaying?

  25. Permalink to comment#

    You can totally tell you’re from Wisconsin. I went to school at Marquette.

  26. Isak
    Permalink to comment#

    Great video! But please, please, please get another microphone or compress the sound… the volume shifts like crazy :(

  27. Permalink to comment#

    has no one gotten the gzinflate data error running the demo files? i found the issue on simplepie bug list but found no solution. any ideas?

    also trying to pull feeds from digg.com wasn’t working for me till i got the entire rss url for digg. anyone had this issue as well? any ideas? chris’s video and others using digg as an example just use digg.com as the url.

  28. Chris,

    Awesome tutorials. I’ve looking for this information forever. Thanks for all your hard work.

    Can I make one recommendation about the video player. The Viddler.com video player enables you to create a video table of contents. So for example if your tutorial has 5 steps. You can creat a Hyperlink for each step and it will play start playing that part of the video at that specific point.

    It took me forever to to find something you said earlier.

    Be well– and thanks for the insightful video tutorials.

  29. Permalink to comment#

    For some reason, when I run the demo files on my server, there are no errors but nothing happens. I have tested the compatibility, and I am running all the required functions.

    Here’s my test page if you want to have a look at it – http://feedme.net23.net/test.php

    Please help :(

  30. Great tutorial! Complete and easy to follow. I’m starting to learn and use SimplePie, and it looks easier when I watched this video. Thank you!

  31. Great video/tutorial. Really easy to follow and I love your humor!

  32. Permalink to comment#

    Hi Chris, great video, very helpful.

    Was wondering if you could shed some light on the error message: cURL error 6: Couldn’t resolve host ‘simplepie.org’

    This code below is what I used, pretty much identical to what you had, but no matter what I put in the feed (an array or just one) it comes up with this error message. I have spent the whole night going through this, trying different settings. Nothing works :-(

    I tried the compatibility test and received: You have everything you need to run SimplePie properly! Congratulations! so that shouldn’t be the problem. Researhing the code on the internet doesn’t come up with anything useful. Most explainatory I could find was:

    This error message indicates that you don’t have a server selected for the account to be setup on. Check you have a default server set in Setup > Servers (denoted by an asterisk *) and that a server is selected on the account that you are trying to setup. (from WHMCS.com)

    I don’t know anything about php, but thought I could show the posts from my own wordpress blog on my (currently) static website made in Dreamweaver.

    Any tips where to look, or even what to ask my server provider what to do would be greatly appreciated!

    Have a great weekend!

    Thanks,
    Jude

    (PS the page below is the entire php page as I want to make sure I get something returned first before I try to apply any style, template, looks etc and this is all it returns: blablabla hhh cURL error 6: Couldn’t resolve host ‘simplepie.org’)

    enable_cache(true);
    $feed->set_cache_location(‘cache’);
    $feed->set_cache_duration(3600);

    $feed->init();

    $feed->handle_content_type();

    ?>

    UngghfhgfhgDocument

    blablabla hhh

    error): ?>
    error; ?>

    get_title(); ?>

    <a href="get_permalink(); ?>”>get_title(); ?>

    get_description(); ?>

  33. Permalink to comment#

    sorry, I just noticed it has made a weird mess of my code :-( I don’t know how to paste the code in here to make it show up like it is in my Dreamweaver page…

  34. Mic Alexander

    Is it possible to have the feeds display, in the same order that I put them in when I defined them in the “$feed->set_feed_url(array(“. And limit them to just the newest feed, one at a time?

  35. Abhi
    Permalink to comment#

    Thanks for the great tutorial. I am trying to fetch feed from multiple sources using the $feed->set_feed_url(array()); However, it does nothing. no error message nothing thrown. the code works seamlessly when trying to process just one feed without using array()

    set_feed_url(array(
    ‘http://feeds2.feedburner.com/CssTricks’,
    ‘http://smashingmagazine.com’,
    ));
    //enable caching
    $feed->enable_cache(true);

    //provide the caching folder
    $feed->set_cache_location(‘../cache’);

    //set the amount of seconds you want to cache the feed
    $feed->set_cache_duration(1800);

    // Run SimplePie.
    $feed->init();

    // This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn’t change it).
    $feed->handle_content_type();

    ?>

    Sample SimplePie Page

    <?php if ($feed->error): ?>
      <p><?php echo $feed->error; ?></p>
    <?php endif; ?>
    
    <div class="header">
        <h1><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h1>
        <p><?php echo $feed->get_description(); ?></p>
    </div>
    
    <?php
    /*
    Here, we'll loop through all of the items in the feed, and $item represents the current item in the loop.
    */
    foreach ($feed->get_items() as $item):
    ?>
    
        <div class="item">
            <h2 style="background:url(<?php $feed = $item->get_feed(); echo $feed->get_favicon(); ?>) no-repeat; text-indent: 25px; margin: 0 0 10px;"><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
            <p><?php echo $item->get_description(); ?></p>
            <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
        </div>
    
    <?php endforeach; ?>
    
    • Abhi
      Permalink to comment#

      did some further digging and figured out that the issue is around trying to fetch the favicon for these feeds.

      <

      h2 style=”background:url(get_feed(); echo $feed->get_favicon(); ?>) no-repeat; text-indent: 25px; margin: 0 0 10px;”>

  36. Brandon
    Permalink to comment#

    Awesome tutorial, although I do not see any .inc files in the new download at simplepie.org I was wondering how to use this with wordpress and what file would be needed to include with the newer version. I also have noticed a simplepie folder in the standard wordpress 3.5.1 install. I have tried everything to get a google calendar xml to parse successfully with the built in rss widget in wordpress. I am hoping to overcome this issue using simplepie and building a custom plugin using it. Please be of any help

    Brandon

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".