nav-left cat-right
cat-right

PHP Universal Feed Parser - lightweight PHP class for parsing RSS and ATOM feeds.

After the PHP Universal Feed Generator, I’ve written the PHP Universal Feed Parser for Orchid Framework. It’s a RSS and ATOM parser written in PHP5. Though there are many feed parsers over Internet, none of those was serving the basic focuses of Orchid: pure object orientation, being lightweight etc. So, I had to write a new one.

UPDATE(15th May, 2008) : cURL support added. Where url fopen() is disabled, the class will use cURL to load the RSS/ATOM content.

Features:

  • Parses all channels and feed item tags and sub tags.
  • Serve the parsed data as associative array.
  • Enough documented and easy to understand code.
  • Many ways to get parsed information.
  • Parsing includes attributes too.
  • No regular expression used.
  • Parsed by XML Parser extension of PHP.
  • Pure PHP5 objected oriented.
  • Enable to parse all commonly used feed versions.

Supported versions: I tried to include all stable and commonly used feed versions. Currently it’s being used to parse the following versions:

  • RSS 1.0
  • RSS 2.0
  • ATOM 1.0

Download:

  • Click Here to get the class file with example. (downloaded 2111 times)
  • Download from phpclasses.org.

How to use:

It’s dead simple to use this class. Just follow this 3 steps:

1. Include the file

include(’FeedParser.php’);

2. Create an object of FeedParser class

$Parser = new FeedParser();

3. Parse the URL you want to featch

$Parser->parse(’http://www.sitepoint.com/rss.php’);

Done.

Now you can use this functions to get various information of parsed feed:

  • $Parser->getChannels() - To get all channel elements as array
  • $Parser->getItems() - To get all feed elements as array
  • $Parser->getChannel($name) - To get a channel element by name
  • $Parser->getItem($index) - To get a feed element as array by it’s index
  • $Parser->getTotalItems() - To get the number of total feed elements
  • $Parser->getFeedVersion() - To get the detected version of parsed feed
  • $Parser->getParsedUrl() - To get the parsed feed URL

A simple example:

Here is a simple example of using this Feed Parser class. Click here to see is the output of this example.

<?php
include('FeedParser.php');
$Parser     = new FeedParser();
$Parser->parse('http://www.sitepoint.com/rss.php');

$channels   = $Parser->getChannels();
$items      = $Parser->getItems();
?>
<h1 id="title"><a href="<?php echo $channels['LINK']; ?>"><?php echo $channels['TITLE']; ?></a></h1>
<p id="description"><?php echo $channels['DESCRIPTION']; ?> </p>

<?php foreach($items as $item): ?>
    <a class="feed-title" href="<?php echo $item['LINK']; ?>"><?php echo $item['TITLE']; ?></a>
    <p class="feed-description"><?php echo $item['DESCRIPTION']; ?></p>
<?php endforeach;?>

I hope, this class is so easy that, anyone who have general knowledge about PHP5 can use it. Whatever it is, Feel free to ask me anything, anytime.


Note : Hi all, I’ve reported about some situations from users where this class is not working properly. So, I’ve decided to re-write it ASAP. Hope the next one will be more powerful, smaller in size and easier to use.
     — Thanks.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • description
  • Wists
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Simpy
  • StumbleUpon
  • Technorati


26 Responses to “PHP Universal Feed Parser - lightweight PHP class for parsing RSS and ATOM feeds.”

  1. [...] PHP Universal Feed Parser - lightweight PHP class for parsing RSS and ATOM feeds. (en inglés). vía: ajaxray [...]

  2. [...] use, and by reading the author’s blog post, this is the intention. So I refer you now to the PHP Universal Feed Parser. I’m not sure how versatile it is, and what could break it yet, but it’s a cool idea [...]

  3. David AUSTRALIA says:

    Have you applied a license to this class? I need to use it for a commercial website… is that OK? :P

  4. admin HONG KONG says:

    @David
    It’s distributed under GPL. So, No problem… u can use it for your commercial website :)

  5. Omar MEXICO says:

    Hi Anis, i use you code but i have a problem, in the description fo the feeds exist HTML code and the tag for p>

    Sorry for my english is so bad. Thanks for help me

  6. StephenB UNITED KINGDOM says:

    Hello
    2 issues:
    1. Doesn’t recognise all feed versions e.g ‘ATOM 1′,
    ‘0.90′ => ‘RSS 1.0′,
    ‘0.91′ => ‘RSS 2.0′,
    ‘0.92′ => ‘RSS 2.0′,
    ‘0.93′ => ‘RSS 2.0′,
    ‘0.94′ => ‘RSS 2.0′,
    ‘1.0′ => ‘RSS 1.0′,
    ‘2.0′ => ‘RSS 2.0′,

    2: There’s an issue in parsing htmlentities. Any feed with e.g <table will have the < character stripped out producing invalid html.

    Any solutions?

    Thanks

  7. StephenB UNITED KINGDOM says:

    that should be < (less than & greater than)

    and doesn’t recognise http://purl.org/atom/ns# for ATOM

  8. hotkto0109 ROMANIA says:

    Has anyone tried parsing a website that requires login?

  9. Sprdnja SERBIA AND MONTENEGRO says:

    Hi,

    This script is great.

    How can I use it to parse multiple rss feeds on one page?

  10. Deamon CROATIA says:

    Tell us how to parse more than one feed ?!!??

  11. Livia GERMANY says:

    Hi, I’m keen to know how to parse more than one feed on a site, too…

    Wanna show my delicious links by tag… so my idea was to fetch the tags and then fetch the items for each tag….

    Can anyone tell me how I do that?

    Thanks a lot in advance :-)

  12. admin HONG KONG says:

    @StephenB, @Omar

    Hope to solve the html entity problem whenever I get some free time.
    Thanks a lot for informing about this problem.
    If someone already solved it, Plz let me know. We all will be greatfull to u.

  13. admin HONG KONG says:

    Hi Livia, Deamon, Sprdnja and others

    Thanks a lot for asking.
    Sorry for being late to reply. Actually I m very busy :(

    It’s very easy to parse multiple rss. Just create new instances of FeedParser class for each URLs.

    You can do it this way:

    $Parser1 = new FeedParser();
    $Parser1->parse(’http://my-first-link/rss’);

    $Parser2 = new FeedParser();
    $Parser2->parse(’http://my-second-link/rss’);
    …………

    If you want to keep all parsed rss together in an array:

    $myFeeds = array();

    $myFeeds['tag1'] = new FeedParser();
    $myFeeds['tag2'] = new FeedParser();
    …..
    $myFeeds['tag1']->parse(’http://tag1-link/rss’);
    $myFeeds['tag2']->parse(’http://tag2-link/rss’);
    …..

    Thanks again

  14. LiviaCH GERMANY says:

    Thank you so very much for your help :-)

  15. Gary says:

    OK, Lets try the HTML Code tag!

    Great Parser..
    How can I use it to parse embedded content (from Wordpress RSS2 feed) like;


    <![CDATA[ .... ]]>

    Thanks

  16. Gary says:

    OK, The PRE tag!

    Great Parser..
    How can I use it to parse embedded content (from Wordpress RSS2 feed) like;

    <![CDATA[ .... ]]>

    Thanks

  17. Dante(Yu) SERBIA AND MONTENEGRO says:

    Thank, it’s a great parser, but i have one problem.
    The parser striped the < from feed description. Have any solution for this problem?

  18. mahad says:

    I would like to parse yahoo’s media tags.. ex. or

    Is it possible?

  19. Gabe UNITED STATES says:

    Rad tool, thanks. One thing though: your script doesn’t handle atom formats that don’t have closing elements. For example, if there is a in the feed it doesn’t find it’s way into the array. Friendfeed uses this extensively. See, http://friendfeed.com/calmebob?format=atom . Any ideas how I can quickly hack this in? I’m not a php expert but willing to try. Thanks again!

  20. dave UNITED STATES says:

    cant fgure out why i get undefined index errors. can anyone help?
    DM

  21. Tareq BANGLADESH says:

    hi, i tried your class for parsing rss. It can handle some rss. But i found that it can’t retrieve rss from many sites like punbb, twitter (that i tried). It show Sorry! cannot detect the feed version.

  22. Anis Ahmad HONG KONG says:

    @Tareq, @dave and more….

    Hi all, I’ve reported about some situations from users where this class is not working properly. So, I’ve decided to re-write it ASAP. Hope the next one will be more powerful, smaller in size and easier to use.

    Thanks for informing me about your problems.

  23. Yaser says:

    Assalam Alaykom Anis. I want to thank you for this class. . its the only class through the whole web I could use..
    but the only problem with it. . it only shows the 1st item in the rss & the loop doesn’t work to show the rest of the rss items.. I wonder if any1 have this problem or I’m doing something wrong. .

    otherwise; waiting for your modified version..

    thank you so much
    ____________
    asalam 3laykom

  24. prem ypi UNITED STATES says:

    This is really awesome. people search for this code everywhere and find only talks but no working code. Thanks for putting up comprehensive code.

  25. ThomasT UNITED STATES says:

    Thanks for the awesome parser.
    Is there a way to set a filter in the parser?
    (like parsing links with match certain key words)

  26. Alex Glover UNITED STATES says:

    How would I use this to display maybe the most recent three entries in the feed?

Leave a Reply