Created
February 22, 2012 12:42
-
-
Save Darkflib/1884880 to your computer and use it in GitHub Desktop.
Generating RSS 2.0 with SimpleXML in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$link=mysql_pconnect($db['write']['host'],$db['write']['user'],$db['write']['pass']) or die ("Could not connect to datadase"); | |
mysql_select_db($db['write']['name']) or die ("could not select database"); | |
//patharray is an essentially an exploded $_SERVER['REQUEST_URI'] | |
//for articles rss feeds it would be /rss/articles/categoryname | |
//so $patharray[0]='rss', $patharray[1]='articles' and $patharray[2]='categoryname' | |
const('SITENAME','Example.com'); | |
const('WEBMASTER','[email protected] ([email protected])'); | |
if (isset($patharray[2])) { | |
$feedname='New articles in '.categorysafetoreal($patharray[2]); | |
} else { | |
$feedname='New articles on '.SITENAME; | |
} | |
$base='http://'.$_SERVER['HTTP_HOST']; | |
$itunesns='http://www.itunes.com/dtds/podcast-1.0.dtd'; | |
$atomns='http://www.w3.org/2005/Atom'; | |
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:atom="http://www.w3.org/2005/Atom" />'); | |
//main page | |
$channel = $xml->addChild('channel'); | |
//atom link - google balks at it... so lets remove it if its google reading it. | |
if (isset($_SERVER['HTTP_USER_AGENT']) and (!stristr($_SERVER['HTTP_USER_AGENT'],'google'))) { | |
$atomlink=$channel->addChild('link','',$atomns); | |
$atomlink->addAttribute('href',$base.$_SERVER['REQUEST_URI']); | |
$atomlink->addAttribute('rel',"self"); | |
$atomlink->addAttribute('type',"application/rss+xml"); } | |
//rss 2.0 stuff | |
$channel->addChild('link',$base.'/'); | |
$channel->addChild('pubDate',gmdate('r')); | |
$channel->addChild('language','en-us'); | |
$channel->addChild('generator','Example.com (http://www.technomonk.com)'); | |
$channel->addChild('webMaster',WEBMASTER); | |
$channel->addChild('description',SITENAME.' - '.$feedname); | |
$channel->addChild('title',$feedname); | |
//itunes | |
if ($script=='podcast') { | |
$owner=$channel->addChild('owner','',$itunesns); | |
$owner->addChild('email',WEBMASTER,$itunesns); | |
$owner->addChild('name','Example.com',$itunesns); | |
$cat=$channel->addChild('category','',$itunesns); | |
$cat->addAttribute('text','Technology'); | |
$subcat=$cat->addChild('category','',$itunesns); | |
$subcat->addAttribute('text','Computers'); | |
$channel->addChild('explicit','no',$itunesns); | |
$channel->addChild('author',SITENAME,$itunesns); | |
} | |
if ($patharray[1]=='articles') { | |
if (isset($patharray[2])) { | |
$query="select author.*, article.summary, UNIX_TIMESTAMP(article.created) as created, article.articleid, article.pagename, article.articletitle, category.categoryname, article.summary from article, author, category where article.authorid=author.authorid and article.categoryid=category.categoryid and category.categorysafename='".mysql_real_escape_string($patharray[2])."' order by article.created DESC limit 10"; | |
} else { | |
$query="select author.*, article.summary, UNIX_TIMESTAMP(article.created) as created, article.articleid, article.pagename, article.articletitle, category.categoryname, article.summary from article, author, category where article.authorid=author.authorid and article.categoryid=category.categoryid order by article.created DESC limit 10"; | |
} | |
$result=mysql_query($query); if ($result) { | |
while ($line=mysql_fetch_assoc($result)) { | |
$item = $channel->addChild('item'); | |
$item->addChild('link',$base.'/article/'.$line['pagename']); | |
$item->addChild('pubDate',gmdate('r',$line['created'])); | |
$item->addChild('title',htmlentities($line['articletitle'])); | |
$item->addChild('description',htmlentities($line['summary'])); | |
$item->addChild('guid',$base.'/guid/article/'.$line['articleid']); | |
} | |
} | |
} | |
//blogentries | |
$query=sprintf("select blogentry.*,unix_timestamp(blogentry.modified) as modified, unix_timestamp(blogentry.postdate) as postdate, blogattachment.filename, blogattachment.filesize, blogattachment.mimetype, blogcategory.categoryname from (blogentry left join blogattachment on blogentry.id=blogattachment.entryid), blogcategory where blogentry.postdate<now() and blogentry.categoryid=blogcategory.id and blogcategory.categoryname='%s' order by blogentry.created desc limit 20",mysql_real_escape_string($script)); | |
$result=mysql_query($query); | |
if ($result) { | |
while ($line=mysql_fetch_assoc($result)) { | |
$item = $channel->addChild('item'); | |
$entryurl=htmlentities(strtolower(str_replace(' ','-',$line['title']))); $item->addChild('link',$base.'/'.$line['categoryname'].'/id/'.$line['id'].'/'.$entryurl); $item->addChild('pubDate',gmdate('r',$line['postdate'])); | |
$item->addChild('title',$line['title']); | |
if (!empty($line['filename'])) { | |
$encl=$item->addChild('enclosure',''); | |
$encl->addAttribute('url',$line['filename']); | |
$encl->addAttribute('length',$line['filesize']); | |
$encl->addAttribute('type',$line['mimetype']); } | |
$item->addChild('description',htmlentities($line['body'])); | |
$item->addChild('guid',$base.'/guid/blog/'.$line['id']); | |
} | |
} | |
header('Content-type: text/xml'); | |
$output=$xml->asXML(); | |
echo $output; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment