Created
February 7, 2012 00:13
-
-
Save onpubcom/1756092 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 | |
// See the following OnpubAPI tutorial for more info: | |
// http://onpubco.com/index.php?articleID=78§ionID=2 | |
// This example is based on an example by Anis uddin Ahmad, the author of | |
// Universal Feed Writer. | |
// Include the required classes. | |
include 'FeedWriter.php'; | |
include 'onpub/api/onpubapi.php'; | |
// Connect to the Onpub database. | |
$pdo = new PDO( 'mysql:host=localhost;dbname=test', 'test', 'test' ); | |
// Connect to the OnpubSections table. | |
$osections = new OnpubSections($pdo); | |
// Specify the query options. | |
// Include articles in the returned section ordered by date, newest to oldest. | |
$queryOptions = new OnpubQueryOptions(); | |
$queryOptions->includeArticles = true; | |
$queryOptions->orderBy = 'created'; | |
$queryOptions->order = 'DESC'; | |
// Get the section from the database. | |
$section = $osections->get(1, $queryOptions); | |
// Get the articles from the section. | |
$articles = $section->articles; | |
//Creating an instance of FeedWriter class. | |
//The constant RSS2 is passed to mention the version | |
$TestFeed = new FeedWriter(RSS2); | |
//Setting the channel elements | |
//Use wrapper functions for common channel elements | |
$TestFeed->setTitle('Onpubco Blog'); | |
$TestFeed->setLink('http://onpubco.com/'); | |
$TestFeed->setDescription(''); | |
//Image title and link must match with the 'title' and 'link' channel elements for RSS 2.0 | |
$TestFeed->setImage('Onpubco Blog','http://onpubco.com/','http://onpubco.com/images/onpubco.png'); | |
//Use core setChannelElement() function for other optional channels | |
$TestFeed->setChannelElement('language', 'en-us'); | |
$TestFeed->setChannelElement('pubDate', date(DATE_RSS, time())); | |
//Adding a feed. Genarally this portion will be in a loop and add all feeds. | |
foreach ($articles as $article) { | |
// Get the article's authors. | |
$authors = $article->authors; | |
//Create an empty FeedItem | |
$newItem = $TestFeed->createNewItem(); | |
//Add elements to the feed item | |
//Use wrapper functions to add common feed elements | |
// Use the OnpubArticle object to set the various properties of the FeedItem. | |
$newItem->setTitle($article->title); | |
$newItem->setLink('http://onpubco.com/tryonpub/template/index.php?sectionID=' . $section->ID . '&articleID=' . $article->ID); | |
//The parameter is a timestamp for setDate() function | |
$newItem->setDate($article->getCreated()->format('c')); | |
$newItem->setDescription($article->content); | |
if (sizeof($authors)) { | |
//Use core addElement() function for other supported optional elements | |
$newItem->addElement('author', $authors[0]->displayAs); | |
} | |
//Now add the feed item | |
$TestFeed->addItem($newItem); | |
} | |
//OK. Everything is done. Now genarate the feed. | |
$TestFeed->genarateFeed(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment