#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:

Comments

  1. User Avatar
    nanochrome
    Permalink to comment#

    Cool Screen cast Safari 4.0 beat is awesome

  2. User Avatar
    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. User Avatar
    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. User Avatar
    Erik Lundmark
    Permalink to comment#

    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. User Avatar
    benjamin
    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.

    • User Avatar
      Todd
      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. User Avatar
    Jared Christensen
    Permalink to comment#

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

  7. User Avatar
    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. User Avatar
    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. User Avatar
    rzea
    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.

    • User Avatar
      Chris Coyier
      Permalink to comment#

      That’s the whole point =)

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

    • User Avatar
      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?)

    • User Avatar
      rzea
      Permalink to comment#

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

      Thanks Xris.

  10. User Avatar
    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. User Avatar
    Ryan Parman
    Permalink to comment#

    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. User Avatar
    Japh
    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. User Avatar
    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. :)

    • User Avatar
      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. User Avatar
    terrytoledo
    Permalink to comment#

    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. User Avatar
    Omar
    Permalink to comment#

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

  16. User Avatar
    insic
    Permalink to comment#

    thank you for this post. It helps a lot.

  17. User Avatar
    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?

    • User Avatar
      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. User Avatar
    Fred Campbell
    Permalink to comment#

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

  19. User Avatar
    Joey
    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. User Avatar
    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. User Avatar
    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!

    • User Avatar
      Chris Coyier
      Permalink to comment#

      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. User Avatar
    tommy
    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. User Avatar
    Gianfranco
    Permalink to comment#

    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.

    • User Avatar
      jjray
      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. User Avatar
    Tresnic
    Permalink to comment#

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

    • User Avatar
      Tresnic
      Permalink to comment#

      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. User Avatar
    Joe
    Permalink to comment#

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

  26. User Avatar
    Isak
    Permalink to comment#

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

  27. User Avatar
    adam a
    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. User Avatar
    Aladine Vargas
    Permalink to comment#

    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. User Avatar
    Alex
    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. User Avatar
    Fren-Dee-Bee
    Permalink to comment#

    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. User Avatar
    Cathy Earle
    Permalink to comment#

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

  32. User Avatar
    Jude
    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. User Avatar
    Jude
    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. User Avatar
    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. User Avatar
    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; ?>
    
    • User Avatar
      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. User Avatar
    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

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag