<?php
/**
* An open source application development framework for PHP5
*
* @author Md. Kausar Alam (kausar_ss2003(at)yahoo.com)
* @since Version 1.0
* file namw feed.class.php
* @filesource
*/
// xmlGenerator class
class feedGenerator
{
//call constractor
function feedGenerator($xml)
{
$this->xmlDoc = new DOMDocument();
$this->xmlDoc->load($xml);
}
/*
* get link/description/title
* eleminate the unwnate char repalceing HTML char
*/
function replaceHTMLCharacter($str)
{
$str = preg_replace('/&/', '&', $str);
$str = preg_replace('/</', '<', $str);
$str = preg_replace('/>/', '>', $str);
$str = preg_replace('/\"/', '"', $str);
$str = preg_replace('/\'/', ''', $str);
return $str;
}
// get header only
function getHeader()
{
$channel = $this->xmlDoc->getElementsByTagName('channel')->item(0);
// get channel title
$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_title = $this->replaceHTMLCharacter($item_title);
// get channel link
$channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $this->replaceHTMLCharacter($channel_link);
// get channel description
$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $this->replaceHTMLCharacter($channel_desc);
//return header
return '<?xml version="1.0" encoding="UTF-8" ?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>'.$channel_title.'</title>
<description>'.$channel_desc.'</description>
<link>'.$channel_link.'</link>
<language>en-us</language>';
}
// main body of xml file here generate
function getBody()
{
$strBody= "";
//get and output "<item>" elements
$x= $this->xmlDoc->getElementsByTagName('item');
//loop will continue untill out site's xml file has finished
for ($i=0; $i<$x->length; $i++)
{
//get title
$item_title = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_title = $this->replaceHTMLCharacter($item_title);
//get link
$item_link = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_link = $this->replaceHTMLCharacter($item_link);
//get description
$item_desc = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $this->replaceHTMLCharacter($item_desc);
$strBody= $strBody . '<item><title>'.$item_title.'</title>
<description>'.$item_desc.'</description>
<link>'.$item_link.'</link>
<guid isPermaLink="true">'.$item_link.'</guid>
</item>';
}
//return main body of xml file
return $strBody;
}
//return footer
function getFooter()
{
return "</channel></rss>";
}
//write all data in xml file
function generateXMLFile()
{
$handle = fopen("feed.xml", "w");
fwrite($handle , $this->getHeader());
fwrite($handle , $this->getBody());
fwrite($handle , $this->getFooter());
fclose($handle);
}
}// end of class
//create object
$feedApp =new feedGenerator("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml");
//write on xml file
$feedApp->generateXMLFile();
header('location:feed.xml');
exit();
?>
|