Last active
February 28, 2023 21:40
-
-
Save pchatterjee/8d696d0bf3ded02f1b021da46bf2f3a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # very important line | |
| @date_default_timezone_set("GMT"); | |
| # open a new MySQL connection using PDO | |
| $pdo = new PDO('mysql:host=localhost;dbname=quotes-db', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); | |
| # formulate the SQL and run it | |
| $sql = 'SELECT `qid`, `quote`, `s-name`, `wpimg` | |
| FROM `quotes`, `source` | |
| WHERE `sid` = `sid-fk` AND MONTH(`ts`) > 1'; | |
| $query = $pdo->prepare($sql); | |
| $query->execute(); | |
| # store all the results in an array | |
| $rss_items = $query->fetchAll(); | |
| # crete a new writer object | |
| $writer = new XMLWriter(); | |
| # output directly to browser | |
| $writer->openURI('php://output'); | |
| # start the document | |
| $writer->startDocument('1.0'); | |
| $writer->setIndent(4); | |
| # declare it as an rss document | |
| $writer->startElement('rss'); | |
| $writer->writeAttribute('version', '2.0'); | |
| $writer->writeAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom'); | |
| $writer->startElement("channel"); | |
| #---------------------------------------------------- | |
| $writer->writeElement('title', 'Quotes from our DB'); | |
| $writer->writeElement('description', 'This is some new quotes from our collection.'); | |
| $writer->writeElement('link', 'http://localhost/quotes/?iqd=new'); | |
| $writer->writeElement('pubDate', date("D, d M Y H:i:s e")); | |
| $writer->startElement('image'); | |
| $writer->writeElement('title', 'Quotes from our DB'); | |
| $writer->writeElement('link', 'http://localhost/link.htm'); | |
| $writer->writeElement('url', 'http://localhost/120x68.png'); | |
| $writer->writeElement('width', '120'); | |
| $writer->writeElement('height', '68'); | |
| $writer->endElement(); | |
| #---------------------------------------------------- | |
| foreach ($rss_items as $item) { | |
| #---------------------------------------------------- | |
| $writer->startElement("item"); | |
| $writer->writeElement('title', $item['quote'] . PHP_EOL . ' - ' . $item['s-name']); | |
| $writer->writeElement('link', 'http://localhost/quotes/?qid=' . 1); | |
| $writer->startElement("description"); | |
| $writer->startCData(); | |
| $writer->text('<img src="' . $item['wpimg'] . '" />'); | |
| $writer->text('<p>more content</p>'); | |
| $writer->endCData(); | |
| $writer->endElement(); | |
| $writer->writeElement('guid', 'http://localhost/quotes/?qid=all'); | |
| $writer->writeElement('pubDate', date("D, d M Y H:i:s e")); | |
| $writer->startElement('category'); | |
| $writer->writeAttribute('domain', 'http://localhost/quotes/'); | |
| $writer->text('May 2008'); | |
| $writer->endElement(); // Category | |
| # end item | |
| $writer->endElement(); | |
| #---------------------------------------------------- | |
| } | |
| # end channel | |
| $writer->endElement(); | |
| # end rss | |
| $writer->endElement(); | |
| $writer->endDocument(); | |
| header('Content-Type: text/xml'); | |
| # send the output to the browser | |
| $writer->flush(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment