Generate RSS Feeds in PHP

More tags

Generate RSS Feeds in PHP
This PHP snippet shows you how to generate RSS Feeds from MySQL database records on the fly. Easy and straightforward.
Author: SimplytheBest Inc Price: Free GPLv2 Type: PHP,MySQL

Assumptions

We use an include to connect to the database. See Connect to MySQL database how to do this.

Code explained

<?php
include "connect.php";

// pass the headers

header("Content-Type: text/xml");
header("Pragma: no-cache");

// RSS version

echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<rss version=\"2.0\">

// channel

<channel>
<title>
This is your title</title>
<description>
This is your description</description>
<link>
This is your link f.e. http://mydomain.com</link>
<language>
en-us</language>
<generator>
Your Website or company name f.e. Bob's RSS Feeds</generator>
";

// let's get the data. This is just an example query. Modify as is needed.

$listing = mysql_query("SELECT *,something.id AS id FROM customers,other_table WHERE other_table.cid=customers.id AND whatever='yes' AND other_table.valid='yes' ");

// Walk through all the records found and grab the data desired

while ($listings = mysql_fetch_assoc($listing)) {
$title = str_replace("&#39;", "'", $
listings['company']);  // here we replace the single quote code back to ' for the browser.
$content = stripslashes(htmlspecialchars($
listings['services']))."\r\n";  // here we convert HTML content back to readable content for the browser. We also add a return because we add another line of content.
$content .= "Address: ".$
listings['address'].", ".$listings['city'].", ".$listings['zip']." ".$listings['state'].", ".$listings['country'];  // here we append another line to the first line of content with ".=" .

If you have just one field of content it would look like:

$content = "Description: ".$listings['description'];  // with a label or simply:
$content = $
listings['description'];  // without a label.

$url = "go.php?id=".$listings['id'] // here we use a go page to redirect to the URL because we want to register the click.

echo "
<item>
<title>
$title</title>
<description>
$content</description>
<link>
$url</link>
<pubDate>
date("l, j F, Y h:i:s A")</pubDate>
<lastBuildDate>
date("l, j F, Y h:i:s A")</lastBuildDate>
</item>
";
//endwhile

echo "
</channel>
</rss>";
?>
 

Code only

<?php
include "connect.php";
header("Content-Type: text/xml");
header("Pragma: no-cache");
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<rss version=\"2.0\">

<channel>
<title>
This is your title</title>
<description>
This is your description</description>
<link>
This is your link f.e. http://mydomain.com</link>
<language>
en-us</language>
<generator>
Your Website or company name f.e. Bob's RSS Feeds</generator>
";

$listing = mysql_query("SELECT *,something.id AS id FROM customers,other_table WHERE other_table.cid=customers.id AND whatever='yes' AND other_table.valid='yes' ");

while ($listings = mysql_fetch_assoc($listing)) {
$title = str_replace("&#39;", "'", $
listings['company']);
$content = stripslashes(htmlspecialchars($
listings['services']))."\r\n";
$content .= "Address: ".$
listings['address'].", ".$listings['city'].", ".$listings['zip']." ".$listings['state'].", ".$listings['country'];
$url = "
go.php?id=".$listings['id'];

echo "
<item>
<title>
$title</title>
<description>
$content</description>
<link>
$url</link>
<pubDate>
date("l, j F, Y h:i:s A")</pubDate>
<lastBuildDate>
date("l, j F, Y h:i:s A")</lastBuildDate>
</item>
";
}

echo "
</channel>
</rss>";
?>

 

See also: RSS 2.0 Specification

Hope this makes you happy enough to link to us! =:)