Generate RSS feeds from your data

<?php
class rss2 extends DOMDocument {
    private $channel;
 
    public function __construct($title, $link, $description) {
        parent::__construct();
        $this->formatOutput = true;
 
        $root = $this->appendChild($this->createElement('rss'));
        $root->setAttribute('version', '2.0');
 
        $channel= $root->appendChild($this->createElement('channel'));
 
        $channel->appendChild($this->createElement('title', $title));
        $channel->appendChild($this->createElement('link', $link));
        $channel->appendChild($this->createElement('description', $description));
 
        $this->channel = $channel;
    }
 
    public function addItem($title, $link, $description) {
        $item = $this->createElement('item');
        $item->appendChild($this->createElement('title', $title));
        $item->appendChild($this->createElement('link', $link));
        $item->appendChild($this->createElement('description', $description));
 
        $this->channel->appendChild($item);
    }
}
 
$rss = new rss2('Channel Title', 'http://www.example.org',
                'Channel Description');
 
$rss->addItem('Item 1', 'http://www.example.org/item1',
              'Item 1 Description');
$rss->addItem('Item 2', 'http://www.example.org/item2',
              'Item 2 Description');
 
print $rss->saveXML();
?>
<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Channel Title</title>
    <link>http://www.example.org</link>
    <description>Channel Description</description>
    <item>
      <title>Item 1</title>
      <link>http://www.example.org/item1</link>
      <description>Item 1 Description</description>
    </item>
    <item>
      <title>Item 2</title>
      <link>http://www.example.org/item2</link>
      <description>Item 2 Description</description>
    </item>
  </channel>
</rss>
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker