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 [downloadcounter(feedparser)] 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.