Recent News

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

Posted by admin on May 2nd, 2008

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 481 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.

PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM)

Posted by admin on March 8th, 2008

It’s been a while since I’ve planned on developing a RSS writer that fulfills most my needs by supporting the various feed formats. Although the necessity was the prime force behind it, a discussion with Hasin and Emran has put the actual fire in. We were discussing about what can be added next in the Orchid - “PHP framework for the rest of us” and suddenly it hit me. At last, it’s finally complete and I’ve named it "PHP Universal Feed Generator", as it generates both ATOM and RSS feeds.

Supported versions:

  • RSS 1.0 (which officially obsoleted RSS 0.90)
  • RSS 2.0 (which officially obsoleted RSS 0.91, 0.92, 0.93 and 0.94)
  • ATOM 1.0

Download: 

Features:

  • Generates RSS 1.0, RSS 2.0 and ATOM 1.0 feeds
  • All feeds are are validated by feed validator.
  • Supports all possible feed elements.
  • Simple and easy to define channel and feed items
  • Implements appropriate namespaces for different versions.
  • Automatically converts date formats.
  • Generates UUID for ATOM feeds.
  • Enables usage of subtags and attributes. (example: image and encloser tags)
  • Completely Object oriented in PHP5 class structure.
  • Handles CDATA encoding for required tags.
  • Nearly same code for generating all kinds of feed

A minimum example

It’s a minimum example of using this class. I am generating a RSS 2.0 feed from retrieved data from a MySQL database. There are more examples in the download package for different versions.

<?php
  // This is a minimum example of using the Universal Feed Generator Class
  include("FeedWriter.php");
  
  //Creating an instance of FeedWriter class. 
  $TestFeed = new FeedWriter(RSS2);
  
  //Setting the channel elements
  //Use wrapper functions for common channel elements
  $TestFeed->setTitle(‘Testing & Checking the RSS writer class’);
  $TestFeed->setLink(‘http://www.ajaxray.com/projects/rss’);
  $TestFeed->setDescription(‘This is test of creating a RSS 2.0 feed Universal Feed Writer’);
  
  //Image title and link must match with the ‘title’ and ‘link’ channel elements for valid RSS 2.0
  $TestFeed->setImage(‘Testing the RSS writer class’,‘http://www.ajaxray.com/projects/rss’,‘http://www.rightbrainsolution.com/images/logo.gif’);
  
    //Retriving informations from database
    mysql_connect("server", "mysql_user", "mysql_password");
    mysql_select_db("my_database");

    $result = mysql_query("Your query here");

 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        //Create an empty FeedItem
        $newItem = $TestFeed->createNewItem();
        
        //Add elements to the feed item    
        $newItem->setTitle($row[‘title’]);
        $newItem->setLink($row[‘link’]);
        $newItem->setDate($row[‘create_date’]);
        $newItem->setDescription($row[‘description’]);
        
        //Now add the feed item
        $TestFeed->addItem($newItem);
    }
  
  //OK. Everything is done. Now genarate the feed.
  $TestFeed->genarateFeed();
?>

Shhhh….a universal feed reader is on the way ;)

Recent Comments | Recent Posts


designed by: Website Builder | Coded by: Blog Directory | Provided by: Wedding photojournalism chicago
bottom