Home arrow MySQL arrow Page 4 - Building a Generic RSS Class With PHP
MYSQL

Building a Generic RSS Class With PHP


Ever wondered how to build an RSS XML feed from your site using PHP? In this article Mitchell explains what RSS is, and steps you through building a generic RSS generating class with PHP and MySQL.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
November 05, 2002
TABLE OF CONTENTS:
  1. · Building a Generic RSS Class With PHP
  2. · What is RSS and How Does It Relate to XML?
  3. · Building Our PHP Class
  4. · The GetRSS Function
  5. · Testing Our Class
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Building a Generic RSS Class With PHP - The GetRSS Function
(Page 4 of 6 )

The GetRSS function of our myRSS class uses string concatenation (joining) to build and return a string that contains XML data, generated from other variables in the class and also the records in our MySQL database, which we created earlier.

The GetRSS function accepts 9 compulsory parameters:
  • $dbServer: The IP/name of your MySQL database server, e.g. localhost.
  • $dbUser: Your MySQL username, e.g. admin.
  • $dbPass: Your MySQL password, e.g. mypass.
  • $dbName: The name of your MySQL database. In our case this is rssDB.
  • $tableName: The table from where records will be extracted. In our case this will be myArticles.
  • $titleFieldName: The name of the MySQL field in our table which will be used as the title element in each item tag. In our case this will be title.
  • $descFieldName: The name of the MySQL field in our table which will be used as the description element in each item tag. In our case this will be summary.
  • $linkFieldName: The name of the MySQL field in our table which will be used to build a link to this article. In our case this will be articleId.
  • $linkTemplate: A string in which the value {linkId} will be replaced with the value from the $linkFieldName field in our MySQL table. In our case we’ve used http://www.mysite.com/articles/{linkId}/, meaning that links will appear in our RSS XML feed as http://www.mysite.com/articles/45/ for example.
GetRSS starts by calling the checkValues function of our class to make sure all channel and image variables have the correct values:

// Connect to the database, generate the RSS XML and return it
function GetRSS($dbServer, $dbUser, $dbPass, $dbName, $tableName, $titleFieldName, $descFieldName, $linkFieldName, $linkTemplate)
{
// Make sure all channel/image values have been set
$this->checkValues();


Next, we build the header, channel and image tags from the class variables:

$rssValue = "<?xml version=\"1.0\"?>\r\n";
$rssValue .= "<!DOCTYPE rss PUBLIC \"-//Netscape Communications//DTD RSS 0.91//EN\" \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">\r\n";
$rssValue .= "<rss version=\"0.91\">\r\n";

// Build the channel tag
$rssValue .= "<channel>\r\n";
$rssValue .= "<title>" . $this->channelTitle . "</title>\r\n";
$rssValue .= "<link>" . $this->channelLink . "</link>\r\n";
$rssValue .= "<description>" . $this->channelDesc . "</description>\r\n";
$rssValue .= "<language>en-us</language>\r\n";
$rssValue .= "</channel>\r\n";

// Build the image tag
$rssValue .= "<image>\r\n";
$rssValue .= "<title>" . $this->imageTitle . "</title>\r\n";
$rssValue .= "<url>" . $this->imageURL . "</url>\r\n";
$rssValue .= "<link>" . $this->imageLink . "</link>\r\n";
$rssValue .= "</image>\r\n";


At this point the header of our RSS XML file is built, and stored safely in a variable called $rssValue. GetRSS now needs to connect to our MySQL database, grab each record in the specified table and turn them into item tags.

We start by connecting the our MySQL database, and making sure the specified table exists by using the mysql_list_tables function:

// Connect to the database and build the <item> tags
$svrConn = @mysql_connect($dbServer, $dbUser, $dbPass) or die("Couldn't connect to database");
$dbConn = @mysql_select_db($dbName, $svrConn) or die("Couldn't select database");

// Make sure the table exists
$tableExists = false;
$tResult = mysql_list_tables($dbName);

while($tRow = mysql_fetch_row($tResult))
{
if(strtolower($tableName) == strtolower($tRow[0]))
{
$tableExists = true;
break;
}
}

if(!$tableExists)
die("Table $tableName doesn't exist in the database!");


If all is well, then we create a MySQL query to grab the top 15 records in the database, ordered by the linkID field (remember that a channel can contain up to 15 items):

$rResult = mysql_query("select $titleFieldName, $descFieldName, $linkFieldName from $tableName order by $linkFieldName asc limit 15") or die("An error occured while retrieving records: " . mysql_error());

As you can see, we substitute the values passed as parameters directly into the query as both table and field names. This makes our query 100% dynamic, and most importantly, generic –- it can be used to rip an RSS XML file from any MySQL database where title, description and link fields exist.

Next, we start a loop, grabbing the values from each MySQL record and appending them to the $rssValue variable as item tags:

// The records were retrieved OK, let's start building the item tags
while($rRow = mysql_fetch_array($rResult))
{
$rssValue .= "<item>\r\n";
$rssValue .= "<title>" . $rRow[$titleFieldName] . "</title>\r\n";
$rssValue .= "<description>" . $rRow[$descFieldName] . "</description>\r\n";
$rssValue .= "<link>" . str_replace("{linkId}", $rRow[$linkFieldName], $linkTemplate) . "</link>\r\n";
$rssValue .= "</item>\r\n";
}


Notice this line in the code above:

$rssValue .= "<link>" . str_replace("{linkId}", $rRow[$linkFieldName], $linkTemplate) . "</link>\r\n";

What this code does, is replace the {linkId} part of the $linkTemplate variable with the ID from the article in our database.

To top things off, we close the RSS tag and return the $rssValue to the caller of the function:

// Add the closing rss tag and return the value
$rssValue .= "</rss>\r\n";
return $rssValue;


All wallah! We have 1 generic PHP RSS XML generating class that can be used to convert any database into a real-time RSS-compatible XML syndication!
blog comments powered by Disqus
MYSQL ARTICLES

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 – More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials