Dom Document in Php is a nice to create an RSS without learning the oddities of bad characters and how to get around them in xml & php. You will still have to filter non-utf characters but the rest of the work is taken care of. Here is a really simple example of an RSS using Dom Document
<?php
header('Content-type: application/rss+xml; charset="utf-8"');
$db = mysqli_db::init();
$articles = $db->fetch_all('SELECT * FROM articles WHERE active = 1 ORDER BY newsdate DESC LIMIT 10');
$rss_link = HTTP_FULLURL . '/rss.php';
$rss_title = SITE_NAME;
$rss_description = SITE_HEAD;
$rss_copyright = SITE_HEAD;
$rss_generator = SITE_NAME . ' RSS Builder';
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
/*
* Rss
*/
$rss = $doc->createElement('rss');
$rss = $doc->appendChild($rss);
$rss->setAttribute('version', '2.0');
/*
* Channel
*/
$channel = $doc->createElement('channel');
$channel = $rss->appendChild($channel);
/*
* Title
*/
$title = $doc->createElement('title');
$title = $channel->appendChild($title);
$title->appendChild($doc->createTextNode($rss_title));
/*
* Link
*/
$link = $doc->createElement('link');
$link = $channel->appendChild($link);
$link->appendChild($doc->createTextNode($rss_link));
/*
* Description
*/
$description = $doc->createElement('description');
$description = $channel->appendChild($description);
$description->appendChild($doc->createTextNode($rss_description));
/*
* Language
*/
$language = $doc->createElement('language');
$language = $channel->appendChild($language);
$language->appendChild($doc->createTextNode('en-us'));
/*
* Publish Date
*/
$pubdate = $doc->createElement('pubDate');
$pubdate = $channel->appendChild($pubdate);
$pubdate->appendChild($doc->createTextNode(date('D, j M Y H:i:s T')));
/*
* Last Build Date
*/
$lastbuilddate = $doc->createElement('lastBuildDate');
$lastbuilddate = $channel->appendChild($lastbuilddate);
$lastbuilddate->appendChild($doc->createTextNode(date('D, j M Y H:i:s T')));
/*
* Copyright
*/
$copyright = $doc->createElement('copyright');
$copyright = $channel->appendChild($copyright);
$copyright->appendChild($doc->createTextNode($rss_copyright));
/*
* Generator
*/
$generator = $doc->createElement('generator');
$generator = $channel->appendChild($generator);
$generator->appendChild($doc->createTextNode($rss_generator));
foreach($articles as $article)
{
/*
* Item
*/
$item = $doc->createElement('item');
$item = $channel->appendChild($item);
/*
* Title
*/
$title = $doc->createElement('title');
$title = $item->appendChild($title);
$title->appendChild($doc->createTextNode($article['title']));
/*
* Link
*/
$params = array();
if(empty($article['page_url']))
{
$params['article_id'] = $article['article_id'];
}
$article['page_url'] = $article['page_url'] ? $article['page_url'] : 'articles-detail.php';
$article['page_url'] .= !empty($params) ? '?' . http::build_query($params) : '';
$link = $doc->createElement('link');
$link = $item->appendChild($link);
$link->appendChild($doc->createTextNode(HTTP_FULLURL . '/' . $article['page_url']));
/*
* Description
*/
$article['teaser'] .= '...';
$description = $doc->createElement('description');
$description = $item->appendChild($description);
$description->appendChild($doc->createCDATASection($article['teaser']));
/*
* Publish Date
*/
$pubdate = $doc->createElement('pubDate');
$pubdate = $item->appendChild($pubdate);
$pubdate->appendChild($doc->createTextNode(date('D, j M Y H:i:s T', strtotime($article['newsdate']))));
/*
* Guid
*/
$guid = $doc->createElement('guid');
$guid = $item->appendChild($guid);
$guid->appendChild($doc->createTextNode(HTTP_FULLURL . '/' . $article['page_url']));
}
echo $doc->saveXML();
?>
« Back to my notebook