Building a Generic RSS Class With PHP - Building Our PHP Class
(Page 3 of 6 )
OOP makes life easier, there's no doubt about that. Programming with objects instead of functions and normal variables is quicker and is also more fun, too. If you're not familiar with OOP in PHP, then have a quick glance over David’s article by clicking
here.
We're going to build a class that will automatically generate a 100% RSS-compliant XML string for us. With this class, you will be able to share and syndicate your site's content and brag to all of your friends that you're down with the new trends in the world of XML.
Let's start by creating a simple MySQL database, where we will store some fictional articles. Drop to the MySQL command window and run the following commands:
create database rssDB;
use rssDB;
create table myArticles
(
articleId int auto_increment primary key,
title varchar(50),
summary varchar(250),
content text,
unique id(articleId)
); We've just created a database and a simple table that will hold some articles. Remember that each item tag in our RSS XML file needs a title, description and link. The title, summary and articleId fields will be useful for this later on.
We now need some articles. Run the following commands at your MySQL console to add some records to our myArticles table:
insert into myArticles values (0, '1st Article', 'This is the summary of my first article', 'This is the body of my first article');
insert into myArticles values (0, '2nd Article', 'This is the summary of my second article', 'This is the body of my second article');
insert into myArticles values (0, '3rd Article', 'This is the summary of my third article', 'This is the body of my third article'); Now, let's start to create our class file. We will build on this code as we go, and a complete version is available in the support material for this article on the last page:
class myRSS
{
// Create our channel variables
var $channelTitle;
var $channelLink;
var $channelDesc;
// Create our image variables
var $imageTitle;
var $imageLink;
var $imageURL; We start by creating our class and calling it myRSS. We also setup 6 class-scoped variables to hold the details that will be used to build our channel and image tags later.
Our class will be very simple, and will contain 2 functions:
function checkValues() This function will make sure that appropriate values have been set for variables such as the channel's title, link and description, as well as the image details.
function GetRSS($dbServer, $dbUser, $dbPass, $dbName, $tableName, $titleFieldName, $descFieldName, $linkFieldName, $linkTemplate) This function is a little more complex. It accepts your MySQL database details, table details and field details and builds an RSS XML string, which it returns.
Let's start by looking at the checkValues function:
function checkValues()
{
// Make sure all channel and image values are set
if($this->channelTitle == "")
die("Please specify a channel title");
if(ereg("$http://", $this->channelLink) == false)
die("Please specify a channel link");
if($this->channelDesc == "")
die("Please specify a channel description");
if($this->imageTitle == "")
die("Please specify an image title");
if(ereg("$http://", $this->imageLink) == false)
die("Please specify an image link");
if(ereg("$http://", $this->imageURL) == false)
die("Please specify an image URL");
} The checkValues function uses some simple string comparison operators and some regular expressions to make sure class variables aren't empty, and that URL's for the image location, etc are prefixed with http://. This function is called from the GetRSS function, which is the main function of our application.
Before we look at the GetRSS function, let's look at how we call our class, assuming that it's complete and all functions are setup. From a new file called test.php, we could use the following code:
include("class.myRSS.php");
// Instantiate the myRSS class
$myRSS = new myRSS;
$myRSS->channelTitle = "My sample channel";
$myRSS->channelLink = "http://www.mysite.com";
$myRSS->channelDesc = "My sample RSS XML channel";
$myRSS->imageTitle = "My sample channel";
$myRSS->imageLink = "http://www.mysite.com/mylogo.gif";
$myRSS->imageURL = "http://www.mysite.com";
// Get the RSS data
$rssData = $myRSS->GetRSS("localhost", "admin", "password", "rssDB", "myArticles", "title", "summary", "articleId", "http://www.mysite.com/articles/{linkId}/");
// Output the generated RSS XML
header("Content-type: text/xml");
echo $rssData; There's nothing fancy about this code. We start by setting the appropriate values for the channel and image attributes, as discussed earlier. We then call the GetRSS function to grab our RSS XML and set a header type to tell the browser we're outputting XML instead of plain text.
Before we look at the GetRSS function, here's a sneak peak of the XML output in IE 6.0:

Next: The GetRSS Function >>
More MySQL Articles
More By Mitchell Harper