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.
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:
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);
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"; }