PHP RSS feed

January 22, 2009 | Download
Tags: php, mysql, howto, rss
With this entry I will show you how to create a PHP based RSS Feed. I am including the database.php file to use as my MySQL connection. First, let's set up a test table and populate with some values.
##
# tables.sql - Shows the table layout used for this example
#
# mysql -u yourusername -D yourdatabasename -p < /path/to/tables.sql
#
# To run from the command line, replace 'yourusername' and 'yourdatabasename' with their 
# respective name, as well as correct the path to your tables.sql file.
#
# Corey Hart @ http://www.codenothing.com
# January 22, 2009
##


#
# Create blogs table
#
DROP TABLE IF EXISTS blogs;

CREATE TABLE blogs(
	blog int unsigned not null auto_increment,
	subject varchar(250) not null,
	body text not null,
	URL varchar(250) not null,
	date_added datetime not null,
	primary key (blog)
);


#
# Populate the blogs table with 2 simple blogs
#
INSERT INTO blogs(subject, body, URL, date_added) VALUES 
	('My First Blog', 'This is me testing my first blog', 'http://www.mysite.com/index.php?blog=1', NOW());
INSERT INTO blogs(subject, body, URL, date_added) VALUES 
	('My Second Blog', 'This is me testing my second blog', 'http://www.mysite.com/index.php?blog=2', NOW());

Now that we have our test case set up, let's take a look at the rss class.

/**
 * RSS.php - Act's as an RSS XML document
 *
 * Corey Hart @ http://www.codenothing.com
 * January 22, 2009
 */ 


// Send the script to the user as an XML document
header('Content-type: text/xml');
// Include database connection
require("/path/to/database.php");


class BlogRssFeed
{
	var $feed; // Hold's the feed


	/* Class Constructor */
	function BlogRssFeed(){
		$this->addFeedTitle(); // RSS Title
		$this->addBlogs(20); // RSS Item's, must declare the maximum amount of item's allowed to send
		$this->closeOffFeed(); // Close off the title tags

		// Once the feed is completed, display it
		echo $this->feed;
	}


	/**
	 * addFeedTitle - Start's the feed off with the required header
	 */ 
	function addFeedTitle(){
		// Add the header
		$this->feed = "<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'><channel>
		<title>My Blog Feed</title>
		<description>This is a feed of My Personal Blog</description>
		<link>http://www.myblogsite.com</link>
		<atom:link href='http://www.myblogsite.com' rel='self' type='application/rss+xml' />";
	}


	/**
	 * addBlogs - Add's the requested number of blogs from the database
	 */ 
	function addBlogs($limit){
		// Variable's outside of the class have to be decalred as global variables
		global $database; 

		// Select the requested number of blogs from the database
		$result = $database->query("SELECT * FROM blogs WHERE ORDER BY blog DESC LIMIT $limit");

		// Add each blog into the feed
		while ($row = mysql_fetch_array($result)){
			$this->feed .= "<item>
			<title> " . $row['subject'] . " </title>
			<description> " . $row['body'] . " </description>
			<link> " .$row['URL'] . "</link>
			<pubDate> " . $this->GMTswitch($row['date_added']) . " </pubDate>
			</item>";
		}
	}	


	/**
	 * closeOffFeed - Does what it says, closes off the tags created at the header
	 */ 
	function closeOffFeed(){
		$this->feed .=  "</channel></rss>";
	}


	/**
	 * Flip the MySQL set date into RSS acceptable date
	 */ 
	function GMTswitch($date){
		list ($y, $m, $d, $h, $min, $s) = split('[- :]', $date);
		return gmdate("D, d M Y H:i:s O", mktime($h, $min, $s, $m, $d, $y));
	}
};

/* Initialize the Class */
new BlogRssFeed;

This class is pretty self-explanatory. I can say is that this is a bare-bones script, in that there is a lot more information you can add to an XML document. Check out the Harvard RSS 2.0 Specification for all the different optional information that can be added.

Download

Latest: rss_php.zip
Released: January 22, 2009