Build Your Own RSS Feed Reader (Posted on March 23rd, 2013)

It's not everyday that a company with a significant market share in a sector gives up that market share. The RSS industry has seen a significant landscape change recently with Google's announcement to power down Reader. Quite a few new feed readers have sprung up recently and several existing ones have been rapidly iterating their software to handle the new influx of users. I'm personally content with using Hacker News and Reddit as my feed readers but I understand people's desire for a more customized experience.

For those that enjoy their feed readers I'm here to offer a solution that you can personalize to no end and will never go away. This post is going to go over building a feed reader in PHP and showing just how simple it can be to aggregate your news to one place where you have full control.

Building Our Feed

The process as you can imagine is pretty simple. Take a list of feeds, get the stories from each feed, sort the combination of stories by date published, then output the results. Let's start by defining our list of feeds.

<?php 
//Feed URLs
$feeds = array(
    "http://maxburstein.com/rss",
    "http://www.engadget.com/rss.xml",
    "http://www.reddit.com/r/programming/.rss",
);
?>

PHP5 comes with a great XML parser called SimpleXML. Let's go ahead and load the feed data into our parser.

//Read each feed's items
$entries = array();
foreach($feeds as $feed) {
    $xml = simplexml_load_file($feed);
    $entries = array_merge($entries, $xml->xpath("//item"));
}

The xpath method on our XML object is basically a string parser. Starting from the root of our XML data it goes down two children and get's all of the children enclosed in an item tag.

Here are the basics of how an RSS feed is structured so you can see why we need to go two children deep to access the item entries.

<rss>
    <channel>
        <title>Feed title</title>
        <link>Link where feed data is pulled from</link>
        <description>Description of feed</description>
        <item>Entry from a feed</item>
        <item>Entry from a feed</item>
        <item>Entry from a feed</item>
        <item>Entry from a feed</item>
        <item>Entry from a feed</item>
    </channel>
</rss>

Now that we have all the entries stored in an array we need to sort them. Otherwise the feeds will show up in the order we have them listed in our feeds array. All my items would be listed first, followed by all the Engadget items, followed by all the Reddit Programming items. All the items should have a pubDate tag that we can sort by. Since we've read all the feed data in as a string we have to remember to convert it to an integer for easy comparison which "strtotime" will take care of for us.

//Sort feed entries by pubDate
usort($entries, function ($feed1, $feed2) {
    return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
});

Our final step is just to output our entries. Each item will usually have at least the following attributes: title, link, and description. So let's use these to build our output.

<ul><?php
//Print all the entries
foreach($entries as $entry){
    ?>
    <li><a href="<?= $entry->link ?>"><?= $entry->title ?></a> (<?= parse_url($entry->link)['host'] ?>)
    <p><?= strftime('%m/%d/%Y %I:%M %p', strtotime($entry->pubDate)) ?></p>
    <p><?= $entry->description ?></p></li>
    <?php
}
?>
</ul>

RSS Feed Reader

That's all that's needed. You can add some CSS styling in if you want to mirror the effect of other feed readers. If you want to play around with the final source code you can fork it from this Github gist or check it out below.

Final Source Code

<html>
    <head>
        <title>RSS Feed Reader</title>
    </head>
    <body>
        <?php
        //Feed URLs
        $feeds = array(
            "http://maxburstein.com/rss",
            "http://www.engadget.com/rss.xml",
            "http://www.reddit.com/r/programming/.rss"
        );
        
        //Read each feed's items
        $entries = array();
        foreach($feeds as $feed) {
            $xml = simplexml_load_file($feed);
            $entries = array_merge($entries, $xml->xpath("//item"));
        }
        
        //Sort feed entries by pubDate
        usort($entries, function ($feed1, $feed2) {
            return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
        });
        
        ?>
        
        <ul><?php
        //Print all the entries
        foreach($entries as $entry){
            ?>
            <li><a href="<?= $entry->link ?>"><?= $entry->title ?></a> (<?= parse_url($entry->link)['host'] ?>)
            <p><?= strftime('%m/%d/%Y %I:%M %p', strtotime($entry->pubDate)) ?></p>
            <p><?= $entry->description ?></p></li>
            <?php
        }
        ?>
        </ul>
    </body>
</html>

As always if you have any feedback or questions feel free to drop them in the comments below or contact me privately on my contact page. Thanks for reading!

Tags: PHP

Comments:

  • Anonymous - 2 years, 10 months ago

    Hi, I tried your reader, but when it loaded, it displayed some weird results. They go along the lines of this (best approximation) * title ?>(link)['host']?>) pubDate))?> description ?> I'm not sure where my error is. Any help would be appreciated.

    reply

  • Max Burstein - 2 years, 10 months ago

    The syntax highlighter uses escaped html characters to display properly. So if you copy and pasted from there it may cause the issue. Try grabbing it off the Github Gist. If that's not the issue then it is possible that the RSS feed(s) you're pulling from are formatted in a weird way. Another issue may be that you have short tags disabled in PHP. Short tags allow rather than having to type out

    reply

  • Sniper Fury Hack - 6 days, 7 hours ago

    This blog is epic!

    reply

  • Paris - 2 years, 10 months ago

    Excellent read, I just passed this onto a colleague who was doing a little study on that. And he really bought me lunch because I discovered it for him smile So let me rephrase that: Thanks for lunch!

    reply

  • Life Insurance Calgary - 4 months, 3 weeks ago

    When someone writes an post he/she maintains the thought of an user in his/her brain that how an user can understand it. Therefore that's why this piece of writing is perfect. Thanks!

    reply

  • Anonymous - 1 year, 11 months ago

    I'm getting this error when I try to run this reader.

    Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/thegreen/www/www/school/CnJ/rss_reader.php on line 32

    I assume its an issue with (<?= parse_url($entry->link)['host'] ?>) but I have no idea why and when my friend runs the same code, his works. Any thoughts?

    reply

  • Max Burstein - 1 year, 11 months ago

    The feed you're parsing may not have one of the fields you're looking for. If you and your friend are using the same code on the same feed then it would have to be the version of PHP that you're running. What feed are you trying to parse?

    reply

  • Anonymous - 1 year, 10 months ago

    Hi. I was trying to use your code but the text editor is giving error report on line: <li><a href="<?= $entry->link ?>"><?= $entry->title ?></a> (<?= parse_url($entry->link)['host'] ?>)

    I'm a bit new to php so pardon for eventual noob comments. Is the '<?=' really meant to be or was a spelling error?

    Thanks

    reply

  • Max Burstein - 1 year, 10 months ago

    The code looks fine to me. <?= is a shortcut for <? echo. Here is more info about it if you're interested http://programmers.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php

    reply

  • Anonymous - 1 year, 4 months ago

    On line 32 the closing ')' in parse_url($entry->link)['host'] ?>) should be after ['host'] like so parse_url($entry->link['host']) ?>) Fix that typo and should work properly.

    reply

  • Anonymous - 1 year, 4 months ago

    The above works but not as intended. Host name only shows up as "(array)". In order to display it properly it needs to look like this. (<?= parse_url($entry->link,PHP_URL_HOST) ?>) Hope this helps someone out there.

    reply

  • St.Petersburg - 1 year, 10 months ago

    Heya! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no data backup. Do you have any solutions to protect against hackers?

    reply

  • Max Burstein - 1 year, 10 months ago

    I have rolling back ups with Heroku as far as data goes. I use Django for this blog so it has a lot of built in security features as does PHP. No system is without flaws I suppose. You just have to learn from your mistakes and move on. With that said moving to a PaaS such as Heroku where server security is more or less handled for you and all you have to take care of is the actual code of your application.

    reply

  • Tony - 1 year, 9 months ago

    With this example could I enter any RSS feed like," http://toledo.craigslist.org/search/ppa?query=furnace&s=0&format=rss" and it will work?

    reply

  • Max Burstein - 1 year, 8 months ago

    Yea it should be fine. Give it a try.

    reply

  • Vidal - 1 month, 3 weeks ago

    The Craigslist feed cannot be parsed, I've just tried, How can we parse this?http://toledo.craigslist.org/search/ppa?query=furnace&s=0&format=rss

    reply

  • Anonymous - 1 year, 6 months ago

    What changes do i have to do to work with this rss?

    http://feeds.feedburner.com/DragoAtMorteFcportoOMelhorClubePortugus

    tks

    reply

  • Peter - 1 year, 4 months ago

    Hi and Yes I found same problem. It is looking like any rss feed going through feedburner from blogspot domain (name_of_blog.blogspot.com) blogs doesn't work. If somebody have his own domain but on blogspot seems work OK (What I can see but I am not sure if it is really for every blog) Is there any way to force to get content from every blogspot blogs?

    reply

  • Peter again - 1 year, 4 months ago

    Heh I just found a solution. For some reason some blogs from feedburner are coded in Atom and if that happen there can't be loaded. What I did I used library from http://atom.geekhood.net/ and on my own serwer re-coded every each of one to Rss2.0 and then my file load to script above. I little bit more hassle but it works

    reply

  • Max Burstein - 1 year, 4 months ago

    Seems weird that some blogs would only support Atom. Nicely done for coming up with a solution on your own.

    reply

  • how to get more followers on instagram - 7 months, 1 week ago

    Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks

    reply

  • instagram comments - 7 months, 1 week ago

    I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!..

    reply

  • sports news - 10 months ago

    Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!!

    reply

  • hungry shark evolution hack - 9 months, 3 weeks ago

    Your music is amazing. You have some very talented artists. I wish you the best of success.

    reply

  • real racing 3 hack - 9 months, 3 weeks ago

    This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine.

    reply

  • PHP Training in Chennai - 9 months, 3 weeks ago

    PHP is a most popular and widely used server-side scripting language for web application development. Statistics reveals that nearly 240 million websites and 3 million web servers were installed with PHP.

    reply

  • PHP Projects in Chennai - 9 months, 3 weeks ago

    PHP developers are among the most sought-after programmers around the world.If you're planning to begin your career in web application development, PLC's PHP final year project would be the first leap towards it.

    reply

  • couponpocket - 8 months, 4 weeks ago

    http://www.couponpocket.com/

    reply

  • Charles M. - 8 months, 2 weeks ago

    Hi there.

    First off, awesome tutorial. I have a question. Is there a way to use both "pubDate" and "dc:date" timestamp. The current setup reads the one with 'dc:date' as 1970... Is there a work around?

    Kindly advice.

    Thank you!

    reply

  • Max Burstein - 7 months, 3 weeks ago

    I'm not sure off the top of my head how to go about reading that format but I'm sure there is a way to go about parsing it.

    reply

  • girlsdoporn - 7 months, 2 weeks ago

    This is really a nice and informative, containing all information and also has a great impact on the new technology.

    reply

  • girlsdoporn - 7 months, 2 weeks ago

    Thanks for sharing this. There are many events which are celebrated all over the world. we have a website about mothers day event so have a look at it.

    reply

  • Salesforce Training in Chennai - 7 months, 2 weeks ago

    Thanks for the article on Build Your Own RSS Feed Reader .Very Informative one.

    reply

  • free instagram followers - 6 months, 3 weeks ago

    Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.

    reply

  • instagram likes - 6 months, 3 weeks ago

    This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.

    reply

  • youtube subscribers - 6 months, 3 weeks ago

    Amazing! Exactly what a watch opener this particular publish may be personally. Greatly valued, saved, We can’t await much more!

    reply

  • comments - 6 months, 3 weeks ago

    Really! Everything that a opener it blog post was for my situation. A great deal enjoyed, bookmarked, As i can’t look ahead to alot more!

    reply

  • green smart living - 6 months, 3 weeks ago

    Such intelligent work on the subject and ideal way of writing here. I am really impressed! This post is a helpful overview of the particular topic and very actionable. Interesting approach!

    reply

  • http://girlfriendactivationsystemreviews.com - 6 months, 3 weeks ago

    Terrific put up, attended on top not to mention saved your web sites. I just can’t hang around to study further because of most people.

    reply

  • learn about debt negotiation - 6 months, 2 weeks ago

    That is thus gorgeous and also imaginative. I recently really like the particular shades and also anyone who receives that inside the email will probably be beaming.

    reply

  • discover more - 6 months ago

    This blog is really great. The information here will surely be of some help to me. Thanks!.

    reply

  • Puja Kumari - 5 months, 3 weeks ago

    Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic http://www.top9th.in/packers-and-movers-bangalore/ http://www.top9th.in/packers-and-movers-hyderabad/ http://www.top9th.in/packers-and-movers-mumbai/ http://www.top9th.in/packers-and-movers-delhi/ http://www.top9th.in/packers-and-movers-gurgaon/ http://www.top9th.in/

    reply

  • Puja Kumari - 5 months, 3 weeks ago

    Thanks for post this helpful post - Please visit for More information about: http://www.top9th.in/packers-and-movers-pune/ http://www.top9th.in/packers-and-movers-noida/ http://www.top9th.in/packers-and-movers-chennai/ http://www.top9th.in/packers-and-movers-kolkata/ http://www.top9th.in/packers-and-movers-ahmedabad/ http://www.top9th.in/packers-and-movers-surat/

    reply

  • Packers Movers - 4 months, 1 week ago

    Thanks for post this helpful post - Please visit for More information about:

    http://www.expert9th.in/packers-and-movers-delhi/ http://www.expert9th.in/packers-and-movers-noida/ http://www.expert9th.in/packers-and-movers-gurgaon/ http://www.expert9th.in/packers-and-movers-kolkata/ http://www.expert9th.in/packers-and-movers-ghaziabad/ http://www.expert9th.in/packers-and-movers-amritsar/


    Thanks for post this helpful post - Please visit for More information about:

    http://www.expert9th.in/packers-and-movers-bangalore/ http://www.expert9th.in/packers-and-movers-hyderabad/ http://www.expert9th.in/packers-and-movers-chennai/ http://www.expert9th.in/packers-and-movers-pune/ http://www.expert9th.in/packers-and-movers-faridabad/ http://www.expert9th.in/packers-and-movers-kochi/


    Thanks for post this helpful post - Please visit for More information about:

    http://www.expert9th.in/packers-and-movers-mumbai/ http://www.expert9th.in/packers-and-movers-surat/ http://www.expert9th.in/packers-and-movers-navimumbai/ http://www.expert9th.in/packers-and-movers-ahmedabad/ http://www.expert9th.in/packers-and-movers-chandigarh/ http://www.expert9th.in/packers-and-movers-jamshedpur/

    reply

  • Packers Movers - 4 months, 1 week ago

    Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic http://www.top9th.in/packers-and-movers-bangalore/ http://www.top9th.in/packers-and-movers-hyderabad/ http://www.top9th.in/packers-and-movers-mumbai/ http://www.top9th.in/packers-and-movers-delhi/ http://www.top9th.in/packers-and-movers-gurgaon/ http://www.top9th.in/


    Thanks for post this helpful post - Please visit for More information about: http://www.top9th.in/packers-and-movers-pune/ http://www.top9th.in/packers-and-movers-noida/ http://www.top9th.in/packers-and-movers-chennai/ http://www.top9th.in/packers-and-movers-kolkata/ http://www.top9th.in/packers-and-movers-ahmedabad/ http://www.top9th.in/packers-and-movers-surat/

    reply

  •  likesplanet php script - 4 months ago

    I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

    reply

  • new year 2016 - 4 months ago

    It is a great article. You will surely like this also because it is a great stufff http://www.happynewyear2016-images.com http://www.recipefrance.com/2015/04/meilleur-images-de-bonne-annee-2016.html http://www.felizanonuevo2016frases.com

    reply

  • labor day mattress sale 2015 - 1 month, 3 weeks ago

    Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing...

    reply

  • labor day mattress sale 2015 - 1 month, 3 weeks ago

    Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing...

    reply

  • adjustable bed frame - 1 month, 3 weeks ago

    I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

    reply

  • best position to sleep - 1 month, 3 weeks ago

    You have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site.

    reply

  • The Best strategy for binary option trading - 1 month, 3 weeks ago

    Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing...

    reply

  • Map Sewa Mobil Surabaya Indonsia Car Rental Hire - 1 month, 1 week ago

    I'm extremely impressed with your writing skills and also with the layout on your "Build Your Own RSS Feed Reader". HAPPY NEW YEAR 2016 HAPPY NEW YEAR 2016 HAPPY NEW YEAR 2016 HAPPY NEW YEAR 2016

    reply

  • Amanda Churchill - 1 month, 1 week ago

    Here is the rss that you have made it tomorrow.

    reply

  • bmw rims for sale - 1 month ago

    Hey – great blog, just looking around some blogs, seems a really nice platform you are using. I’m currently using WordPress for a few of my blogs but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?

    reply

  • Curious Team - 3 weeks, 3 days ago

    This really good job. It's working but. I want to know can it read any type of feed?

    reply

  • hack for clash of clans . pixel gun 3d hack ifunbo - 2 weeks, 6 days ago

    Starting from the root of our XML data it goes down two children and get's all of the children enclosed in an item tag.

    reply

  • asaad - 2 weeks, 6 days ago

    Your Site Is Very Good and The Post is Well On Topic, Thanks for Sharing it with us.

    <a href="http://songs2k6.blogspot.com/">New Filmi Songs 2016</a> <a href="http://news2016.edublogs.org/2016/01/04/4-new-elements-are-added-to-the-periodic-table/">4 new elements are added to the periodic table</a> <a href="http://news2016.hpage.com/">latest entertainment news 2016</a> <a href="http://news2016.jigsy.com/">Miley Cyrus and Chris Hamsworth</a> <a href="http://vurl.com/W6V5o">spigen iphone 7</a> <a href="http://psl16.blogspot.com/">Karachi Kings Highlights PSL 2016</a> <a href="http://www.urdupoetry4u.com/2016/01/best-urdu-poetry-2016-bismil-ke-tadapne.html">best urdu poetry 2016</a> <a href="http://www.newsongshd.com">Latest Hindi Songs 2016</a> <a href="https://www.youtube.com/watch?v=jPUtmBUQ5nw">copyright free music for youtube 2016</a>

    reply

  • the best mattress - 2 weeks, 3 days ago

    Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing...

    reply

  • towelroot v2 - 2 weeks, 1 day ago

    Can you do a version of this comic in your style? With all due respect to Ralph Steadman and Quentin Blake, I prefer your way of drawing.

    reply

  • best bed reviews - 2 weeks ago

    Hey – great blog, just looking around some blogs, seems a really nice platform you are using. I’m currently using WordPress for a few of my blogs but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?

    reply

  • insta followers ! instalike apk - 1 week, 3 days ago

    This post is going to go over building a feed reader in PHP and showing just how simple it can be to aggregate your news to one place where you have full control.

    reply

  • David - 1 week, 2 days ago

    cdsasfda

    reply

  • jacff - 1 week, 2 days ago

    dsfsdf http://www.maxburstein.com/

    reply

  • Jual Laptop Murah - 1 week, 2 days ago

    Your blog place during etc issue! I do know the provider the fact that improved my family prepare any by chance! Setting up, so why have to we should suffer through planning to prepare him or her us anytime one can find individuals who will assistance individuals with each other? That i mention, now let him or her assistance.

    reply

  • trickyindia - 5 days, 7 hours ago

    Thanks of this article, now i am going to build rss feed

    reply

  • AppCoiner PDF - 2 days, 19 hours ago

    Thanks for providing step-by-step instructions about building RSS feed

    reply