db-feed-sample.php Explained – Sample Feed Database Integration Php Script

January 5th, 2012
by Daniel Soutter

The db-feed-sample.php example script can be used to help generate a feed from information stored in a MySQL database.  The sample script has been configured to use a separate config file that contains general information about the feed (channel) as well as mappings of feed elements to columns in a database table.

When the feed is constructed, the feed details set in the the config file are used to populate the feed channel elements, which contain information about the overall feed and not individual feed items.  The database query can either be set manually, or generated dynamically using the column mappings provided in the config file.  The column mappings are also used to obtain specific information from each associative database record.

Details of the config file and feed scripts are below, which includes additional comments to help with configuring a feed populated from database records.  The contents of the scripts are also provided in full at the end of this post, or you can download the database feed integration example scripts explained here as a zip package.

A step by step tutorial is also available to assist with using this sample script.

feed-config.php
This is the configuration file for the database feed.  The components of this file are detailed below in the order that they appear in the file.

Set up and create the connection to the database
By setting the connection details in each of the parameters that are then used to create the connection to the database.

This is the configuration file for the database feed.  The components of this file are detailed below in the order that they appear in the file.

Set up and create the connection to the database
By setting the connection details in each of the parameters that are then used to create the connection to the database.

/*Set up database connectivity
* -------------------------------
* Create a connection to the database
* --------------------------------- */
$feed_db_host = 'dbhost';
$feed_db_port = '3306'; //default MySQL port
$feed_db_user = 'dbuser';
$feed_db_pass = 'dbpassword';
$feed_db_dbname = 'feeditems';

Database column mappings
In this section, an associative array is created that provides the column mappings of the database used when populating feed items with data.  The mappings are also used to generate the query to retrieve items from the database if a custom query is not used.  A custom query can be set near the bottom of this config file, which is explained in more detail later.

Specify the name of the table in the database that contains the feed items.

$feed_items_table = 'articles';

Specify a limit for the maximum number of items to retrieve from the database.

$feed_item_limit = 100; //the maximum number of items to retrieve.  Set as null to ignore limit

Supply the field mappings.  The value of each element in the array below should be set to the name of the relative column in the database table.

 

$feed_item_table_cols = Array(
   'item_id_column' =>         'item_id',            //DB Record ID column
   'item_title_column' =>         'item_title',
   'item_content_column' =>     'item_body',
   'item_date_column' =>         'date_modified',     //To be set as last updated and used as order by column.  Assumed column type "TIMESAMP"
   'item_link_column' =>         null,                //In this example, the link is generated using a base url and the record ID so NULL is set for this column.  If a column name is entered instead of NULL, the value from this column will be used to set the feed link.
   'item_copyright_column' =>     null,                 //example NULL value indicating that there is no column containing copyright details for each item.
   'item_author_name_column' => 'item_author_name',
   'item_category_column' =>     'category'
);

The parameter below can be used to specify a base url used to generate the URL to the page that each feed item corresponds to.  You can also use the item_link_column mapping in the array above if the full url to the page for each feed item is stored in the database record.  If the item_link_column mapping is set to NULL, the base url parameter below will be used to generate the feed item link.

$item_link_base_url = "http://phpfeedwriter.webmasterhub.net/?id="; //In this example, this is the base url used to generate the link to a feed item.

Feed Channel Data
In this section of the file, general information about the feed is provided.  This information is included at the top of the feed, and is separate to the items added to the feed.  Comments in the code should explain what type of information should be supplied in each parameter.  You can also refer to the online documentation for additional information about these elements.

 

//Channel Data for valid output in all supported formats
//----------------------------------
//Feed Title
$feed_title = 'Php FeedWriter Sample Feed from Database';

//Feed Link
$feed_url = 'http://phpfeedwriter.webmasterhub.net/';

//Feed Description
$feed_description = 'Generate an Atom 1.0, RSS 2.0 and other feeds from website or database content';

//Channel Image
$channel_image = Array(
   'imageTitle' => 'WebmasterHub.net',
   'imageSrc' => 'http://www.webmasterhub.net/img/logo.jpg',
   'imageUrl' => 'http://www.webmasterhub.net/' );

//Channel self link (link to the current feed)
$feed_selfLink = 'http://phpfeedwriter.webmasterhub.net/samples/db-feed-sample.php';

//Feed Author details
$feed_author = Array(
   'name' => 'Daniel Soutter',
   'url' => 'http://phpfeedwriter.webmasterhub.net/',
   'email' => null ); //format [email protected], null if no email provided

//Feed Last Updated
$date_feed_updated = '2011-12-11T00:00:00Z'; //ISO 8601 Fromat

//Feed Language
$feed_Language = 'EN-US';

//Optionl Channel Data
//-----------------------------

//Feed ID (unique identifier for the feed, set to feed link if not provided)
$feed_id = 'http://phpfeedwriter.webmasterhub.net/';

//Feed Publish Date
$date_feed_published = '2011-12-11T00:00:00Z'; //ISO 8601 Fromat

//Feed Webmaster (Contact for technical information)
$feed_webmaster = '[email protected] (First Last)';

//Feed Copyright Owner
$feed_copyright_notice = '(c) Daniel Soutter';

//Feed Categories
$feedCategories = Array('Php Feed Generator', 'Database RSS Feed');

//Skip Days (specify days of the week that feed readers can skip checking for updates)
$skip_days = Array('Saturday', 'Sunday');

//Skip Hours
$skip_hours = Array(22,23,0);

//Feed Input (example values only / not real)
$feed_input = Array(
   'input_title' => 'Search',
   'input_desc' => 'Search items in this feed',
   'input_name' => 'Search',
   'input_url' => 'http://phpfeedwriter.webmasterhub.net/'    );

//Feed Cloud
$feed_cloud = Array(
   'cloud_domain' => 'phpfeedwriter.webmasterhub.net',
   'cloud_port' => '80',
   'cloud_path' => '/rss_cloud',
   'cloud_reg_procedure' => 'pingMe',
   'cloud_protocol' => 'soap'    );

//Feed Contributor
$feed_contributor = Array(
   'name' => 'Daniel Soutter',
   'url' => 'http://phpfeedwriter.webmasterhub.net/',
   'email' => null ); //format [email protected], null if no email provided

Set the database select query, or set to NULL to allow the script to generate the query from the column mappings supplied above.

$select_query = null;

 

 

db-feed-sample.php
This is the mail controller script the uses the feed-config.php file (above) and the Php feedWriter class to generate a feed from records in the MySQL database.  A breakdown of the script is as follows:

Include the config file containing the details of the feed and database.

include('feed-config.php');

Include the Php FeedWriter class file

require_once("FeedWriter.php");

Instantiate a new FeedWriter object from the Php FeedWriter class.  The title, description and link are provided to the class constructor using parameters set in the config file.

$feed = new FeedWriter(
       $feed_title,
       $feed_description,
       $feed_url,
       6, //indent
       true, //Use CDATA
       null, //encoding
       true //enable validation
       );

Enable debug mode to have the class include additional information about error or validation errors to help create a valid feed.

$feed->debug = true;

Create the database connection using the values set in the config file.

//Connect to the database
   if (!$dbConnection = mysql_connect($feed_db_host . ':' . $feed_db_port, $feed_db_user, $feed_db_pass))
   {
       die('Error Connecting To Database');
   }
   else //Connected successfully
   {
       //    Select the database
       if (!mysql_select_db($feed_db_dbname, $dbConnection)) {
           die('Error selecting database');
       }
   }

The following code block sets the default output format to display the feed in, which is RSS 2.0.  It then takes the value from the ‘format’ query string parameter if available and sets the output format to the value from the query string if it corresponds to one of the constants used to identify each format.

$format = RSS_2_0;
   if(isset($_GET['format']) && $_GET['format'] != null)
   {
       foreach($feed->getFeedFormats() as $curFormat)
       {
           if($curFormat[0] == $_GET['format'])
               $format = $curFormat[1];
       }
   }

The next block generates the select query used to retrieve items from the database. If the select query is set manually in the feed-config.php file, this step is ignored and the custom query used instead.

if($select_query == null)
   {
       $colsIncluded = 0;
       $select_query = "SELECT ";

       //Loop through table columns and add to query if not null
       foreach($feed_item_table_cols as $curCol){
           if($colsIncluded == 0 && $curCol != null){
               //First
               $select_query .= $curCol;
               $colsIncluded++;
           }
           elseif($curCol != null){
               $select_query .= ", $curCol";
               $colsIncluded++;
           }
       }
   }
   //Append remain components of query
   $select_query .= " FROM $feed_items_table ";
   //Order By
   $select_query .= "ORDER BY " . $feed_item_table_cols['item_date_column'] . " DESC ";
   //Row Limit
   if($feed_item_limit != null && $feed_item_limit > 0)
       $select_query .= " LIMIT 0, $feed_item_limit ";
   //Finish query
   $select_query .= "; ";

The following block executes the database query using the database connection created above and the select query.

if(isset($dbConnection) && $dbConnection != false){
       if(!($mysql_result = mysql_query($select_query, $dbConnection))){
           //Display the database error.
           if($feed->debug)
               die("<b>Error " . mysql_errno() . "</b><br/>" . mysql_error());
           else
               exit;
       }
       //Store the associative record set in an array.
       while(($feedItems[] = mysql_fetch_assoc($mysql_result)) || array_pop($feedItems));
   }
   else {
       die("Connection unavailable...");
   }

In the next section, the channel data is added to the feed.  The first block of function calls include information that is required to generate a valid feed in RSS 1.0, RSS 2.0 and Atom 1.0 formats. Some formats require that different information is included in the feed channel, but it is recommended that each of the following elements are populated to ensure that all feed output formats can be used and will be valid.

$feed->set_image($channel_image['imageTitle'], $channel_image['imageSrc'], $channel_image['imageUrl']);
   $feed->set_selfLink($feed_selfLink);
   $feed->set_author($feed_author['email'], $feed_author['name'],$feed_author['url']);
   $feed->set_date($date_feed_updated,DATE_UPDATED);
   $feed->set_language($feed_Language);

The next block of channel data elements are optional, which can be used to include additional information about the feed as well as instructions for feed readers on how to use the feed.

   $feed->set_webmaster($feed_webmaster);
   $feed->set_skipDays($skip_days);
   $feed->set_skipHours($skip_hours);
$feed->set_input($feed_input['input_title'],$feed_input['input_desc'],$feed_input['input_name'],$feed_input['input_url']);
$feed->set_cloud($feed_cloud['cloud_domain'],$feed_cloud['cloud_port'],$feed_cloud['cloud_path'],$feed_cloud['cloud_reg_procedure'], $feed_cloud['cloud_protocol']);
   $feed->set_date($date_feed_published,DATE_PUBLISHED);
   $feed->set_id($feed_id);
   $feed->set_copyright($feed_copyright_notice);
   $feed->add_contributor($feed_contributor['email'], $feed_contributor['name'],$feed_contributor['url']);

One or more categories can be added to the feed Channel by calling the add_category function to add each category.

//Add categories to the channel
   foreach($feedCategories as $curCategory) {
       $feed->add_category($curCategory);
   }

In the next section, items are added to the feed if the number of records obtained from the database is greater than 0.  A foreach loop is used to loop through the record set and add a feed item for each of the records.

The data that is available in each record set is first set to various parameters parameters that are then used to populate the feed items with data.  This is to allow customisation of the data included in each feed item through various conditions, as well as to cater for cases where some data is not available for some elements of the feed item.  An example condition is used below to set the item link element to a custom URL if the record set doesn’t explicitly contain an item link URL.

if(count($feedItems) > 0)
   {
       foreach($feedItems as $curItem)
       {
           //obtain davailable for the current feed item.
           //----------------------------
           //The example has been configured this way to ease the process of
           //adjusting the database column mappings to feed item elements, and to make the example more generic.
           //As a result, this process could be simplified if required to suit more specific needs.  

           if($feed_item_table_cols['item_id_column'] != null)
               $item_id = $curItem[$feed_item_table_cols['item_id_column']];
           else
               $item_id = null;    

           if($feed_item_table_cols['item_title_column'] != null)
               $item_title = $curItem[$feed_item_table_cols['item_title_column']];
           else
               $item_title = 'Untitled';

           if($feed_item_table_cols['item_content_column'] != null)
               $item_body = $curItem[$feed_item_table_cols['item_content_column']];
           else
               $item_body = null;

           if($feed_item_table_cols['item_date_column'] != null)
               $date_modified = $curItem[$feed_item_table_cols['item_date_column']];
           else
               $date_modified = null;

           if($feed_item_table_cols['item_link_column'] != null)
               $item_link = $curItem[$feed_item_table_cols['item_link_column']];
           elseif($item_link_base_url != null) {
               $item_link = $item_link_base_url . $item_id;
           }
           else
               $item_link = null;

           if($feed_item_table_cols['item_copyright_column'] != null)
               $item_copyright = $curItem[$feed_item_table_cols['item_copyright_column']];
           else
               $item_copyright = null;

           if($feed_item_table_cols['item_author_name_column'] != null)
               $item_author_name = $curItem[$feed_item_table_cols['item_author_name_column']];
           else
               $item_author_name = null;

           //Example assumes a single category is stored in each feed item record.
           if($feed_item_table_cols['item_category_column'] != null)
               $category = $curItem[$feed_item_table_cols['item_category_column']];
           else
               $category = null;

           //Add the current item to the feed
           $feed->add_item($item_title, $item_body, $item_link);

           //Add the feed item modified date if available
           if($date_modified != null)
               $feed->set_date($date_modified, DATE_UPDATED);

           //Set the ID of the current feed item to the link if not null
           if($item_link != null)
               $feed->set_id($item_link);

           //Add copyright details if available
           if($item_copyright != null)
               $feed->set_copyright($item_copyright);

           //Add the author name if supplied - set_author(email, name, uri)
           if($item_author_name != null)
               $feed->set_author(null, $item_author_name, null);

           //Set the comments url to the link for the feed item (common, but optional)
           if($item_link != null)
               $feed->set_comments($item_link);

           //Add the category to the item.  This function should be called multiple times to add more than one category to a single feed item.
           $feed->add_category($category);
       }
   }

The next section adjusts the output format of the feed item content element in all feed items to allow HTML to be rendered by a browser or feed reader when viewing the feed.

$feed->set_feedConstruct($format);
$feed->feed_construct->construct['itemContent']['type'] = 'html';

The final line of code calls the getXML() function, which generates the feed XML in the specified format (determined above), and sends the output to the browser.

echo $feed->getXML($format);

 

 

The code from each file is provided below in full, or can be downloaded as a zip package as well.

Download the sample scripts to generate a Feed from database records.

feed-config.php – full script

<?php
//--------------------------------------------------------------------------------
//	Php FeedWriter 3.2 - Sample Usage Feed Configuration File
//
//  This file contains variables used to configure a feed and database connection
//  required by the db-feed-sample.php Php FeedWriter sample script.
//
//	(c) Copyright Daniel Soutter
//
//	Website: http://phpFeedWriter.WebmasterHub.net/
//
//  Php FeedWriter and this sample script is provided with no warranty and may be
//  used or developed at your own risk, providing that you have read and aggree to
//	the Terms of Use http://phpfeedwriter.webmasterhub.net/terms .
//
//	Please post any comments, bugs or suggestions for improvement to the website.
//
//	For usage instructions or technical information about Php FeedWriter, see the
//  online documentation: http://phpFeedWriter.WebmasterHub.net/docs
//--------------------------------------------------------------------------------

/*Set up database connectivity
 * -------------------------------
 * Create a connection to the database
 * --------------------------------- */
$feed_db_host = 'dbhost';
$feed_db_port = '3306'; //default MySQL port
$feed_db_user = 'dbuser';
$feed_db_pass = 'dbpassword';
$feed_db_dbname = '';

/*Database select query field names
 * -------------------------------
 * In this section, a range of values are set to be used when mapping data from each
 * row in the database query recordset to elements in feed items.  These create a more generic example of mapping
 * database table columns to elements in the feed based on the assumptions below that can be more easily adjusted.
 * If the mapppings below are not applicable to your database columns or table relationships, you may need to
 * customise this section in the config file and the process in the main controller file to suit your needs.
 *
 * The values below are also used to construct the database query to retrieve items from the database.
 *
 * Assumptions made for the purpose of this example:
 * - names set below exist as a column in the table containing the feed item data.
 * - data required to populate a feed item is availble in each record.  If this
 *   is not how your items table is configured, you may need to customise the process of populating feed item data
 *   in the db-feed-sample.php file to suit your needs (eg. generating the feed item link using a base url and item ID).
 *
 * There are also additional feed item elements available using Php FeedWriter that are not inlcuded in this example.
 * Please see the online doucmentation for a full list of the feed item elements and corresponding functions used to populate with data.
 *
 * NULL should be supplied for columns that are not available in the database table.
 * --------------------------------- */

$feed_items_table = 'articles';
$feed_item_limit = 100; //the maximum number of items to retrieve.  Set as null to ignore limit

$feed_item_table_cols = Array(
	'item_id_column' => 		'item_id',			//DB Record ID column
	'item_title_column' => 		'item_title',
	'item_content_column' => 	'item_body',
	'item_date_column' => 		'date_modified', 	//To be set as last updated and used as order by column.  Assumed column type "TIMESAMP"
	'item_link_column' => 		null,				//In this example, the link is generated using a base url and the record ID so NULL is set for this column.  If a column name is entered instead of NULL, the value from this column will be used to set the feed link.
	'item_copyright_column' => 	null, 				//example NULL value indicating that there is no column containing copyright details for each item.
	'item_author_name_column' => 'item_author_name',
	'item_category_column' => 	'category'
);

$item_link_base_url = "http://phpfeedwriter.webmasterhub.net/?id="; //In this example, this is the base url used to generate the link to a feed item.  

/*Feed Channel data
 * -------------------------------
 * In this section a number of variables are created to store
 * information that will be used to populate the feed channel data.
 *
 * Change the sample values to the information about your feed.
 *
 * Please see the Php FeedWriter Online documentation ( http://phpFeedWriter.WebmasterHub.net/docs )
 * for details about which elements are required and which are optional for the various output formats.
 * --------------------------------- */

//Channel Data for valid output in all supported formats
//----------------------------------
//Feed Title
$feed_title = 'Php FeedWriter Sample Feed from Database';

//Feed Link
$feed_url = 'http://phpfeedwriter.webmasterhub.net/';

//Feed Description
$feed_description = 'Generate an Atom 1.0, RSS 2.0 and other feeds from website or database content';

//Channel Image
$channel_image = Array(
	'imageTitle' => 'WebmasterHub.net',
	'imageSrc' => 'http://www.webmasterhub.net/img/logo.jpg',
	'imageUrl' => 'http://www.webmasterhub.net/' );

//Channel self link (link to the current feed)
$feed_selfLink = 'http://phpfeedwriter.webmasterhub.net/samples/db-feed-sample.php';

//Feed Author details
$feed_author = Array(
	'name' => 'Daniel Soutter',
	'url' => 'http://phpfeedwriter.webmasterhub.net/',
	'email' => null ); //format [email protected], null if no email provided

//Feed Last Updated
$date_feed_updated = '2011-12-11T00:00:00Z'; //ISO 8601 Fromat

//Feed Language
$feed_Language = 'EN-US';

//Optionl Channel Data
//-----------------------------

//Feed ID (unique identifier for the feed, set to feed link if not provided)
$feed_id = 'http://phpfeedwriter.webmasterhub.net/';

//Feed Publish Date
$date_feed_published = '2011-12-11T00:00:00Z'; //ISO 8601 Fromat

//Feed Webmaster (Contact for technical information)
$feed_webmaster = '[email protected] (First Last)';

//Feed Copyright Owner
$feed_copyright_notice = '(c) Daniel Soutter';

//Feed Categories
$feedCategories = Array('Php Feed Generator', 'Database RSS Feed');

//Skip Days (specify days of the week that feed readers can skip checking for updates)
$skip_days = Array('Saturday', 'Sunday');

//Skip Hours
$skip_hours = Array(22,23,0);

//Feed Input (example values only / not real)
$feed_input = Array(
	'input_title' => 'Search',
	'input_desc' => 'Search items in this feed',
	'input_name' => 'Search',
	'input_url' => 'http://phpfeedwriter.webmasterhub.net/'	);

//Feed Cloud
$feed_cloud = Array(
	'cloud_domain' => 'phpfeedwriter.webmasterhub.net',
	'cloud_port' => '80',
	'cloud_path' => '/rss_cloud',
	'cloud_reg_procedure' => 'pingMe',
	'cloud_protocol' => 'soap'	);

//Feed Contribotor
$feed_contributor = Array(
	'name' => 'Daniel Soutter',
	'url' => 'http://phpfeedwriter.webmasterhub.net/',
	'email' => null ); //format [email protected], null if no email provided

//Manually set the Select Query to retrieve feed items from the database
//If $select_query is NULL, the query is generated dynamically from the table column mappings set above.
$select_query = null;

?>

db-feed-sample.php – full script

<?php
	//--------------------------------------------------------------------------------
	//	Php FeedWriter 3.2 - Sample Usage
	//
	//	(c) Copyright Daniel Soutter
	//
	//	Website: http://phpFeedWriter.WebmasterHub.net/
	//
	//  Php FeedWriter and this sample script is provided with no warranty and may be
	//  used or developed at your own risk, providing that you have read and aggree to
	//	the Terms of Use http://phpfeedwriter.webmasterhub.net/terms .
	//
	//	Please post any comments, bugs or suggestions for improvement to the website.
	//
	//	For usage instructions or technical information about Php FeedWriter, see the
	//  online documentation: http://phpFeedWriter.WebmasterHub.net/docs
	//
	//  The feed and database configuration details for this example are set in
	//  the separate feed-config.php file provided as part of this sample.
	//
	//--------------------------------------------------------------------------------

	// Include the config file containing variables for feed channel data and the database connection / object.
	include('feed-config.php');

	//Include FeedWriter class file
	//--------------------------------
	require_once("FeedWriter.php");

	//1. Instantiate new FeedWriter object, passing the title, description and link
	//--------------------------------
	$feed = new FeedWriter(
		$feed_title,
		$feed_description,
		$feed_url,
		6, //indent
		true, //Use CDATA
		null, //encoding
		true //enable validation
		);

	//Enable debug
	$feed->debug = true;

	//Connect to the database
	if (!$dbConnection = mysql_connect($feed_db_host . ':' . $feed_db_port, $feed_db_user, $feed_db_pass))
	{
		die('Error Connecting To Database');
	}
	else //Connected successfully
	{
		//	Select the database
		if (!mysql_select_db($feed_db_dbname, $dbConnection)) {
			die('Error selecting database');
		}
	}

	//Get the format to display in,
	//set to RSS 2.0 as default or if format is not found
	$format = RSS_2_0;
	if(isset($_GET['format']) && $_GET['format'] != null)
	{
		foreach($feed->getFeedFormats() as $curFormat)
		{
			if($curFormat[0] == $_GET['format'])
				$format = $curFormat[1];
		}
	}

	//Build the SELECT query if not manually set in the config file.
	//----------------------------------
	//If $select_query has been manually set in theconfig file to a custom select query, the custom query will be used.
	//If $select_query is set to NULL in the config file, the query is generated dynamically.
	if($select_query == null)
	{
		$colsIncluded = 0;
		$select_query = "SELECT ";

		//Loop through table columns and add to query if not null
		foreach($feed_item_table_cols as $curCol){
			if($colsIncluded == 0 && $curCol != null){
				//First
				$select_query .= $curCol;
				$colsIncluded++;
			}
			elseif($curCol != null){
				$select_query .= ", $curCol";
				$colsIncluded++;
			}
		}
	}

	//Append remain components of query
	$select_query .= " FROM $feed_items_table ";
	//Order By
	$select_query .= "ORDER BY " . $feed_item_table_cols['item_date_column'] . " DESC ";
	//Row Limit
	if($feed_item_limit != null && $feed_item_limit > 0)
		$select_query .= " LIMIT 0, $feed_item_limit ";
	//Finish query
	$select_query .= "; ";

	//echo $select_query;

	//Retrieve the records from the database to be added as items to the feed.
	//Check that the DB Connection variable has been instantiated (by the config file - feed-config.php)
	if(isset($dbConnection) && $dbConnection != false){
		if(!($mysql_result = mysql_query($select_query, $dbConnection))){
			//Display the database error.
			if($feed->debug)
				die("<b>Error " . mysql_errno() . "</b><br/>" . mysql_error());
			else
				exit;
		}
		//Store the associative record set in an array.
		while(($feedItems[] = mysql_fetch_assoc($mysql_result)) || array_pop($feedItems));
	}
	else {
		die("Connection unavailable...");
	}	

	//End of Feed Configuration --- 

	//Start populating the channel data and feed items with data:
	//---------------------------------
	// The variables containing channel data are set in the feed-config.php file which comes as part of
	// this sample script.

	//Add channel data to the feed
	$feed->set_image($channel_image['imageTitle'], $channel_image['imageSrc'], $channel_image['imageUrl']);
	$feed->set_selfLink($feed_selfLink);
	$feed->set_author($feed_author['email'], $feed_author['name'],$feed_author['url']);
	$feed->set_date($date_feed_updated,DATE_UPDATED);
	$feed->set_language($feed_Language);

	$feed->set_webmaster($feed_webmaster);
	$feed->set_skipDays($skip_days);
	$feed->set_skipHours($skip_hours);
	$feed->set_input($feed_input['input_title'],$feed_input['input_desc'],$feed_input['input_name'],$feed_input['input_url']);
	$feed->set_cloud($feed_cloud['cloud_domain'],$feed_cloud['cloud_port'],$feed_cloud['cloud_path'],$feed_cloud['cloud_reg_procedure'], $feed_cloud['cloud_protocol']);
	$feed->set_date($date_feed_published,DATE_PUBLISHED);
	$feed->set_id($feed_id);
	$feed->set_copyright($feed_copyright_notice);
	$feed->add_contributor($feed_contributor['email'], $feed_contributor['name'],$feed_contributor['url']);

	//Add categories to the channel
	foreach($feedCategories as $curCategory) {
		$feed->add_category($curCategory);
	}

	//Add items to the feed
	//--------------------------------
	if(count($feedItems) > 0)
	{
		foreach($feedItems as $curItem)
		{
			//obtain data availble for the current feed item.
			//----------------------------
			//The example has been configured this way to ease the process of
			//adjusting the database column mappings to feed item elements, and to make the example more generic.
			//As a result, this process could be similified if required to suit more specific needs.

			if($feed_item_table_cols['item_id_column'] != null)
				$item_id = $curItem[$feed_item_table_cols['item_id_column']];
			else
				$item_id = null;	

			if($feed_item_table_cols['item_title_column'] != null)
				$item_title = $curItem[$feed_item_table_cols['item_title_column']];
			else
				$item_title = 'Untitled';

			if($feed_item_table_cols['item_content_column'] != null)
				$item_body = $curItem[$feed_item_table_cols['item_content_column']];
			else
				$item_body = null;

			if($feed_item_table_cols['item_date_column'] != null)
				$date_modified = $curItem[$feed_item_table_cols['item_date_column']];
			else
				$date_modified = null;

			if($feed_item_table_cols['item_link_column'] != null)
				$item_link = $curItem[$feed_item_table_cols['item_link_column']];
			elseif($item_link_base_url != null) {
				$item_link = $item_link_base_url . $item_id;
			}
			else
				$item_link = null;

			if($feed_item_table_cols['item_copyright_column'] != null)
				$item_copyright = $curItem[$feed_item_table_cols['item_copyright_column']];
			else
				$item_copyright = null;

			if($feed_item_table_cols['item_author_name_column'] != null)
				$item_author_name = $curItem[$feed_item_table_cols['item_author_name_column']];
			else
				$item_author_name = null;

			//Example assumes a single category is stored in each feed item record.
			if($feed_item_table_cols['item_category_column'] != null)
				$category = $curItem[$feed_item_table_cols['item_category_column']];
			else
				$category = null;

			//Add the current item to the feed
			$feed->add_item($item_title, $item_body, $item_link);

			//Add the feed item modified date if available
			if($date_modified != null)
				$feed->set_date($date_modified, DATE_UPDATED);

			//Set the ID of the current feed item to the link if not null
			if($item_link != null)
				$feed->set_id($item_link);

			//Add copyright details if available
			if($item_copyright != null)
				$feed->set_copyright($item_copyright);

			//Add the author name if supplied - set_author(email, name, uri)
			if($item_author_name != null)
				$feed->set_author(null, $item_author_name, null);

			//Set the comments url to the link for the feed item (common, but optional)
			if($item_link != null)
				$feed->set_comments($item_link);

			//Add the category to the item.  This function should be called multiple times to add more than one category to a single feed item.
			$feed->add_category($category);
		}
	}

	//Set the item body/content output type to html for the selected output format
	$feed->set_feedConstruct($format);
	$feed->feed_construct->construct['itemContent']['type'] = 'html';

	//Build and output the feed XML in the specified, or default format
	//--------------------------------
	echo $feed->getXML($format);
?>

 

 

 

Tags: , , ,
Posted in FeedWriter Class | Comments (0)

  • Php FeedWriter (c) Copyright 2011 Daniel Soutter. Php FeedWriter and the information on this site has been designed to benefit website developers, owners and users, but may contain errors and is provided as is, with no warranty. Terms.