Creating an XML RSS feed with PHP.

Using this PHP class you can create an RSS feed of your dynamic content very easily.

I have built the class for this site so it is focused on blog style content. I have built in XSLT support into the class. The class will print out the XML header the content and then the XML footer. I have put all the files you need into a folder: rss.zip.

RSS Class (class.rss.php)

class rssFeed
{

function rss($input, $stylesheet="")
{ 
	is_array($input) ? '' : error('this function requires input to be an array');
	
 	$input["encoding"] = (empty($input["encoding"])) ? "UTF-8" : stripslashes($input["encoding"]);
	$input["language"] = (empty($input["language"])) ? "en-us" : stripslashes($input["language"]);
	$input["title"] = stripslashes($input["title"]);
	$input["description"] = stripslashes($input["description"]);
	$input["link"] = stripslashes($input["link"]);
	$rss = '';
    $rss .= ($stylesheet != "" ? "\n" . '' : "");
	$rss .= <<<__header__



{$input["title"]}
{$input["description"]}
{$input["link"]}
{$input["language"]}

__header__;
    foreach($input["items"] as $item)
    {
		$datetime = strtotime($item["pubDate"]);
		$datetime = date("D, d M Y H:i:s",$datetime);
		$item["description"] = strip_tags($item["description"]);
	
		$rss .= "\n\n" . stripslashes($item["title"]) . "";
        $rss .= "\n" . trunc($item["description"], 40) . "";
        if (!empty($item["pubDate"]))  $rss .= "\n" . $datetime . " GMT";
        if (!empty($item["link"])) $rss .= "\n".stripslashes($item["link"]) . "";
        $rss .= "\n\n";
    }
    $rss .= "\n\n";
	return $rss;
}

}

The first function "rss()" has two variables, "input" which expects an array of data and "stylesheet", optional and can be used to link to your XSL file.

The function then processes the input array checking through each key value. The function then starts building $rss - the rss string. building the header text, checking if a stylesheet has been given.

The second function in the class, processes through each item in your items array adding the relevant XML tags for each section of content. I use one of my library functions to truncate the description text to the first sentence.

The final two lines adds the end tags for channel and rss to the $rss variable and then returns the variable.

So how do you implement this class into your RSS page?

Example page (rss-example-page.php)

include('library.php');
include('class.rss.php');

$rss = new rssFeed;

$sql = "SELECT * FROM table-name";

$article_array = execute_query($sql);

$itemsarray = array();

foreach($article_array as $item){
	$itemsarray[] = array("title"=> $item['title'], "description"=> $item['description'], "pubDate"=>$item['pubDate'], "link"=>"http://www.paulwest.co.uk/article.php/".$item['link']);
}

$rss_array = array(
	"encoding"=>"UTF-8",
	"language"=>"en-us",
	"title"=>"Paul West's articles RSS Feed", // This field is mandatory
	"description"=>"Paul West's development and design articles", // This field is mandatory
	"link"=>"http://www.paulwest.co.uk/", // This field is mandatory
	"items"=>$itemsarray
);

header("Content-type: application/xml");
echo $rss->rss($rss_array, "rss.xsl"); // Second parameter is not required

I include the class and library files needed for our tasks then I initiate the rss class. do the usual MySQL query to grab my data using my library function that data is returned as an array. I then run through that array creating the $itemsarray and adding the values to that array. This step could be removed if you didn't need to add any extra data and your column titles matched those needed in the class. I have it here as I add the site URL rather than adding it in the Class which would make the class specific to my site. I then build the $rss_array and submit it to the rss class and rss function which returns the XML string.

Download Files

Published 03.03.09