Sunday 6 June 2010

RSS Feeds with XML Format.

< ?php
header('Content-type: text/xml');

/* Creating OBJECT of included class file from module*/
$article=new article();

echo '< ?xml version="1.0" encoding="utf-8"?>';
echo '< rss version="2.0" xml:base="'.SITE_URL.'" xmlns:dc="http://purl.org/dc/elements/1.1/">';
echo '< channel>';
echo '< title>Site Title< /title>';
echo '< link>http://www.grabhost.net< /link>';
echo '< description>Grab Host Articles List< /description>';
/* Sample code for fetching the object for feeds. */
$articles = $article->get_articles_for_rss_feed();

foreach ( $articles as $article_array)
{ /* Converting timestamp format datetime value to standard RSS format Date Time Format. */
$getdate = $article_array['timestamp'];
if(!empty($getdate)){
$getdate = explode(" ", $getdate);
$getdate = explode("-", $getdate[0]);
$gettime = explode(":", $getdate[1]);
$gettime[0] = !empty($gettime[0]) ? $gettime[0] : 0;
$gettime[1] = !empty($gettime[1]) ? $gettime[1] : 0;
$gettime[2] = !empty($gettime[2]) ? $gettime[2] : 0;
$getdate[0] = !empty($getdate[0]) ? $getdate[0] : 0;
$getdate[1] = !empty($getdate[1]) ? $getdate[1] : 0;
$getdate[2] = !empty($getdate[2]) ? $getdate[2] : 0;
$getdate = mktime($gettime[0], $gettime[1], $gettime[2], $getdate[1] , $getdate[2], $getdate[0]);
$getdate = date("D, d M y H:i:s O", $getdate);
}

echo '';
echo '< title>< ![CDATA[' . $article_array['title'] . ']]>< /title>';
echo '< link>' . SITE_URL . $article_array['seo_title'] . '< /link>';
echo '< description>< /description>';
echo '< comments>' . SITE_URL . $article_array['seo_title'] . '#comments< /comments>';
echo '< pubDate>'.$getdate.'< /pubDate>';
echo '< dc:creator/>';
echo '< guid isPermaLink="false">'.getrandnum(4).' at '.SITE_URL.''; // getrandnum(4) generate the random value having length four (4)
echo '< /item>';
}

echo '< /channel>';
echo '< /rss>';

// RSS Feeds RSS 2.0 with Standard XML format.
?>

RSS Feeds with XML Format.

< ?php
header('Content-type: text/xml');

/* Creating OBJECT of included class file from module*/
$article=new article();

echo '< ?xml version="1.0" encoding="utf-8"?>';
echo '< rss version="2.0" xml:base="'.SITE_URL.'" xmlns:dc="http://purl.org/dc/elements/1.1/">';
echo '< channel>';
echo '< title>Site Title< /title>';
echo '< link>http://www.grabhost.net< /link>';
echo '< description>Grab Host Articles List< /description>';
/* Sample code for fetching the object for feeds. */
$articles = $article->get_articles_for_rss_feed();

foreach ( $articles as $article_array)
{ /* Converting timestamp format datetime value to standard RSS format Date Time Format. */
$getdate = $article_array['timestamp'];
if(!empty($getdate)){
$getdate = explode(" ", $getdate);
$getdate = explode("-", $getdate[0]);
$gettime = explode(":", $getdate[1]);
$gettime[0] = !empty($gettime[0]) ? $gettime[0] : 0;
$gettime[1] = !empty($gettime[1]) ? $gettime[1] : 0;
$gettime[2] = !empty($gettime[2]) ? $gettime[2] : 0;
$getdate[0] = !empty($getdate[0]) ? $getdate[0] : 0;
$getdate[1] = !empty($getdate[1]) ? $getdate[1] : 0;
$getdate[2] = !empty($getdate[2]) ? $getdate[2] : 0;
$getdate = mktime($gettime[0], $gettime[1], $gettime[2], $getdate[1] , $getdate[2], $getdate[0]);
$getdate = date("D, d M y H:i:s O", $getdate);
}

echo '';
echo '< title>< ![CDATA[' . $article_array['title'] . ']]>< /title>';
echo '< link>' . SITE_URL . $article_array['seo_title'] . '< /link>';
echo '< description>< /description>';
echo '< comments>' . SITE_URL . $article_array['seo_title'] . '#comments< /comments>';
echo '< pubDate>'.$getdate.'< /pubDate>';
echo '< dc:creator/>';
echo '< guid isPermaLink="false">'.getrandnum(4).' at '.SITE_URL.''; // getrandnum(4) generate the random value having length four (4)
echo '< /item>';
}

echo '< /channel>';
echo '< /rss>';

// RSS Feeds RSS 2.0 with Standard XML format.
?>

Creating RSS Feeds with Plain display......

Here is the code for creating RSS Feeds in plain XML Feed format..
< ?php
header('Content-type: text/xml');

$feed_contents = "< !--XML Format RSS Feed for Website -- >";
$feed_contents .= "< listing>";
$feed_contents .= "< item>< name>Name 1 here< address>Address 1 here< /address>< /item>";
$feed_contents .= "< item>< name>Name 2 here< /name>< address>Address 2 here< /address>< /item>";
$feed_contents .= ''< /listing >";

$xml_filename = 'feed.xml'; // give the xml file path here
/* deleting the XML file if exists. */
if (file_exists($xml_filename)) {
unlink($xml_filename);
}
/* Writing the XML file. */
$file_handle = fopen($xml_filename, 'a');
fwrite($file_handle, $feed_contents);
fclose($file_handle);

echo $feed_contents;

?>