Top

RSS Feed


PHP Code

Please Register to submit score.
Bookmark and Share
Average Score  7.0 (of 3 scores)
Date Added  Sep 07, 2004
Last Updated  Sep 15, 2008

Description

This small snippet will create an RSS feed. I've tested the format using several popular RSS readers and it seems to work fine.

Grab the Code

<?php
 
// Some RSS readers don't like certain characters in the text so this function will replace them with HTML entities
function escape_upper_chars($text) {
	// 32-90, 97-122
	for ($x = 0; $x < strlen($text); $x++) {
		$w = $text{$x};
		if ((ord($w) < 32) || ((ord($w) > 90) && (ord($w) < 97)) || (ord($w) > 122)) {
			$q .= '&#'.ord($w).';';
		} else {
			$q .= $w;
		}
	}
	return $q;
}
 
header("Content-type: text/xml");
 
// Send the header data
echo "<?xml version=\"1.0\"?><!DOCTYPE rss SYSTEM \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">
	<rss version=\"0.91\">
	<channel>
	<title>RSS Feed</title>
	<language>en</language>
	<link>http://www.website.com</link>
	<description>RSS Feed Description</description>\n";
 
// Force int data type on $limit to prevent SQL injection attacks
$limit = (int)$_GET['limit'];
 
if (($limit < 1) || ($limit > 50)) { $limit = 15; }  // Set the default number of results
 
// Query the database (make sure you have included the necessary commands to connect to a MySQL server)
$query = "SELECT id,title,time,author
		FROM articles
		ORDER BY time DESC
		LIMIT $limit";
 
$s = mysql_query($query);
 
// Change the variables here according to your query above
while (list($id,$title,$time,$author) = mysql_fetch_row($s)) {
 
	$title = escape_upper_chars($title);
 
	// Customize the data sent to the RSS reader. You should pass text grabbed from the database through escape_upper_chars()
 
	echo "<item>\n"; // Begin a new item
	echo "<title><![CDATA[$title]]></title>\n"; // Send the title
	echo "<link><![CDATA[http://www.website.com/view.php?id=$id]]></link>\n"; // Send link to the item
	echo "<description><![CDATA[Posted by $author on " . date("l dS of F @ h:ia",$time) . "]]></description>\n"; // Send description
	echo "</item>\n"; //End the item
 
}
 
echo "</channel>\n</rss>"; // Close the RSS channel
 
?>

Comments

  (7)  RSS
piotrek
Comments: 3
 
PHP Snippet:  RSS Feed
Posted Jan 07, 2005
this code is almost perfect.. I have only one poblem with it.. it does strange stuff with some Polish fonts.. instead of "ł" it places &179; .. can you fix that ?
tye
Comments: 67
 
PHP Snippet:  RSS Feed
Posted Jan 08, 2005
I would think the problem is with the RSS reader. A fix might be to replace the code inside the 'escape_upper_chars' function with: return $text;
piotrek
Comments: 3
 
PHP Snippet:  RSS Feed
Posted Jan 09, 2005
I am using Mozilla Thunerbird and Firefox (in bookmarks...)
piotrek
Comments: 3
 
PHP Snippet:  RSS Feed
Posted Jan 09, 2005
ok I changed that and added encoding line and it words fine now....
Hawkee
Comments: 1,122
 
PHP Snippet:  RSS Feed
Posted May 29, 2005
Cool snippet tye, definitely something that can be used.
xyz
Comments: 2
 
PHP Snippet:  RSS Feed
Posted Jul 08, 2005
A little food for thought..

<?php
/* RSS feeds */

if (($fd1 = @fopen("rss.xml", "w")) && ($fd2 = fopen("rssdd.xml", "w")))
{
$cats = "";
$res = mysql_query("SELECT id, name FROM categories");
while ($arr = mysql_fetch_assoc($res))
$cats[$arr["id"]] = $arr["name"];
$s = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n<rss version=\"0.91\">\n<channel>\n" .
"<title>Torrenfilez</title>\n<description>torrents</description>\n<link>$DEFAULTBASEURL/</link>\n";
@fwrite($fd1, $s);
@fwrite($fd2, $s);
$r = mysql_query("SELECT id,name,descr,filename,category FROM torrents ORDER BY added DESC LIMIT 15") or sqlerr(__FILE__, __LINE__);
while ($a = mysql_fetch_assoc($r))
{
$cat = $cats[$a["category"]];
$s = "<item>\n<title>" . htmlspecialchars($a["name"] . " ($cat)") . "</title>\n" .
"<description>" . htmlspecialchars($a["descr"]) . "</description>\n";
@fwrite($fd1, $s);
@fwrite($fd2, $s);
@fwrite($fd1, "<link>$DEFAULTBASEURL/details.php?id=$a[id]&amp;hit=1</link>\n</item>\n");
$filename = htmlspecialchars($a["filename"]);
@fwrite($fd2, "<link>$DEFAULTBASEURL/download/$a[id]/$filename</link>\n</item>\n");
}
$s = "</channel>\n</rss>\n";
@fwrite($fd1, $s);
@fwrite($fd2, $s);
@fclose($fd1);
@fclose($fd2);
}
?>
lostdeviant
Comments: 9
 
PHP Snippet:  RSS Feed
Posted Jul 17, 2008
I modified this code a little to better learn PHP (I'm a PHP newbie). Since I currently have each datafeed source in a separate table in my database, I can add a ?x=tablename to pull a feed from that table instead of from the database in general. The description area is modified to work with CSV files from Shareasale, but you could just replace the table names with those from your network.

<?php
// Some RSS readers don't like certain characters in the text so this function will replace them with HTML entities
function escape_upper_chars($text) {
// 32-90, 97-122
for ($x = 0; $x < strlen($text); $x++) {
$w = $text{$x};
if ((ord($w) < 32) || ((ord($w) > 90) && (ord($w) < 97)) || (ord($w) > 122)) {
$q .= '&#'.ord($w).';';
} else {
$q .= $w;
}
}
return $q;
}

$x = mysql_real_escape_string($_GET[x]);
if(!$x) { $x = 'exampletablename'; }


header("Content-type: text/xml");

// Send the header data
echo "<?xml version=\"1.0\"?><!DOCTYPE rss SYSTEM \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">
<rss version=\"0.91\">
<channel>
<title>RSS Feed</title>
<language>en</language>
<link>http://www.website.com</link>
<description>RSS Feed Description</description>\n";

// Force int data type on $limit to prevent SQL injection attacks
$limit = (int)$_GET['limit'];

if (($limit < 1) || ($limit > 50)) { $limit = 20; } // Set the default number of results

// Query the database (make sure you have included the necessary commands to connect to a MySQL server)
$query = "SELECT Name,Merchant,Link,BigImage,Price,Category,SubCategory,Description,Manufacturer,MerchantCategory,MerchantSubcategory,Num
FROM $x
ORDER BY Num DESC
LIMIT $limit";

$s = mysql_query($query);

// Change the variables here according to your query above
while (list($name,$merchant,$link,$bigimage,$price,$category,$subcategory,$description,$manufacturer,$merchantcategory,$merchantsubcategory,$num) = mysql_fetch_row($s)) {

$title = escape_upper_chars($name);
$description = escape_upper_chars($description);

// Customize the data sent to the RSS reader. You should pass text grabbed from the database through escape_upper_chars()

echo "<item>\n"; // Begin a new item
echo "<title><![CDATA[$title]]></title>\n"; // Send the title
echo "<link><![CDATA[$link]]></link>\n"; // Send link to the item
echo "<description><![CDATA[<a href='$link'><IMG SRC='$bigimage' alt='$title' align='left' border='o' hspace='10px'></a> $title<br>$description $$price<p>$category - $subcategory - $merchantcategory - $merchantsubcategory From $merchant $manufacturer]]></description>\n"; // Send description
echo "</item>\n"; //End the item

}

echo "</channel>\n</rss>"; // Close the RSS channel

?>

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  

Bottom