RSS template for PHP
I recently added a RSS feed to blog.nani-so
it was surprisingly easy to do, I simply had to reuse my SQL query from the index page and ended up with this:
<?php header ( "Content-type: text/xml" ) ; include '../static.nani-so.re/db.php'; $sql = "SELECT id, title, content, date AS full_date FROM blog WHERE visible = 1 ORDER BY full_date DESC LIMIT 10"; $req = $cnx->prepare($sql); $req->execute(); $rss = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>"; $rss .= "<rss version=\"2.0\">"; $rss .= "<channel>" ; $rss .= "<title>nani-so.re</title>"; $rss .= "<link>https://nani-so.re</link>" ; $rss .= "<description>stuff</description>" ; while ($data=$req->fetch()) { $date = date("D, d M Y H:i:s", strtotime($data['full_date'])); $rss .= "<item>" ; $rss .= "<title><![CDATA[".$data['title']."]]></title>"; $rss .= "<link>https://blog.nani-so.re/post?id=".$data['id']."</link>" ; $rss .= "<description><![CDATA[".htmlspecialchars($data['content'])."]]></description>" ; $rss .= "<pubDate>".$date." GMT</pubDate>" ; $rss .= "</item>" ; } $rss .= "</channel>" ; $rss .= "</rss>" ; echo $rss; ?>
I've also forked a project I found on HN, the author was kind enough to share the code to setup a feed page that aggregates from multiple RSS sources