working with RSS feeds

Soldato
Joined
1 Feb 2006
Posts
8,188
guys,

doing a content management system in php/mysql and have to include some way of providing rss feeds of any articles that are added to my cms..

what is the best way to do this?

Can i just output from the DB right into an xml file?

How can i select which articles I want to keep in the feeds i.e. the most recent 20 articles?

An option would be to use an xml storage file rather than a database but i think i would needmysql with the php for controlling logins/session variables etc..

im a newbie to php/mysql but have experienced limited uses of XML.

Currently I am learning about XML DOM transformations and then we will look at Xpath..maybe this will be useful?

Great place to post here and I know some v smart ppl live on here!

Thanks in advance
J
 
Hopefully this will help

Code:
<?php

	include 'db.php';

	$sql = mysql_query("SELECT * FROM `rss` ORDER BY `id` DESC LIMIT 0, 15");

	$xml = "<?xml version=\"1.0\"?>
	<rss version=\"2.0\">
		<channel>
			<title>Title of Feed</title>
			<description>Decription of Feed</description>
			<link>Http://www.linktowebsite.com</link>";	

	while($result = mysql_fetch_array($sql)){
	$title = $result['title'];
	$description = $result['description'];
	$link = $result['link'];
	$xml .= "
		<item>
			<title>$title</title>
			<description>$description</description>
			<link>$link</link>
		</item>";
	}

	$xml .= "
		</channel>
	</rss>";

	$file = fopen("index.xml", 'w+') or die("can't open file");
	fwrite($file, $xml);
	fclose($file);

	echo 'RSS Feed Updated';

?>
 
haha, is it really that simple?! hadn't thought about doing that til now. cheers conrad :)
 
Hi Guys,

Resurrecting an old thread as I have the same problem. I am Trying to get an rss feed from some database Fields.

I am pretty new to php having had Dreamweaver do the work for me in the past. In Conrads example above he includes the files db.php I am assuming that this is the connection to the database can anyone give me an example of what this would look like.

Or give me any ideas of how to get a database driven page into an Rss format

Cheers

Shorty
 
Database connection would be something along the lines of

Code:
$hostname = "localhost";
$user = "username";
$password = "password";
$database = "database";
//Connect to the host using the above details
$connection = mysql_connect($hostname, $user, $password) or die("Error could not connect to the host.");
//Connect to the databse using the above connection
mysql_select_db($database, $connection) or die("Error could not connect to the database.");

Then you could have something like

Code:
<?php
require_once "db.php";
require "make_safe.php";

header('Content-type: text/xml');
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n";
echo "<?xml-stylesheet title=\"XSL_formatting\" type=\"text/xsl\" href=\"/shared/bsp/xsl/rss/nolsol.xsl\"?>\r\n";
echo "<rss version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss\">\r\n";

echo "<channel>\r\n";

echo "<title>Title</title>\r\n";
echo "<link>http://www.website.com/</link>\r\n";
echo "<description>Description</description>\r\n";

$query = "SELECT rss_title, rss_link FROM rss WHERE rss_active='1' ORDER BY rss_flow DESC";
$result = mysql_query($query) or die(mysql_error());
$nor = mysql_num_rows($result);

if($nor > 0)
{
	while($row = mysql_fetch_assoc($result))
	{
		$rt = $row["rss_title"];
		$rl = $row["rss_link"];
		echo "<item>\r\n";
		echo "<title>" . xml_ready($rt) . "</title>\r\n";
		echo "<link>" . xml_ready($rl) . "</link>\r\n";
		echo "</item>\r\n";
	}// while($row = mysql_fetch_assoc($result))
}// if($nor > 0)

echo "</channel>\r\n";
echo "</rss>\r\n";
?>

The make_safe.php file contains a function called xml_ready that converts any characters that would cause an issue raw in XML to HTML.

Code:
function xml_ready($unsafe)
{
$unsafe = str_replace("&","&amp;",$unsafe);
$unsafe = str_replace("<","&lt;",$unsafe);
$unsafe = str_replace(">","&gt;",$unsafe);
$unsafe = str_replace("\"","&quot;",$unsafe);
$unsafe = str_replace("\'","&apos;",$unsafe);
return $unsafe;
}

You'll note in the php it throws out

Code:
header('Content-type: text/xml');

which is actually saying this is a txt/xml file. In my htaccess file I use this as well

Code:
# RSS XML
# domain/latest.xml to domain/latest.php
RewriteRule ^latest\.xml$ latest.php [QSA,L]

which means I can even give an xml ending address instead of the php one.

Hope that helps
 
Ok got most of it working but getting an error when visiting http://www.rainchasers.com/rss/latest.php

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

A name was started with an invalid character. Error processing resource 'http://www.rainchasers.com/rss/latest.php'. Line ...

$unsafe = str_replace("&","&amp;",$unsafe);
------------------------^

Any suggestions ?
 
Your make_safe.php should be:
Code:
<?php
function xml_ready($unsafe)
{
$unsafe = str_replace("&","&amp;",$unsafe);
$unsafe = str_replace("<","&lt;",$unsafe);
$unsafe = str_replace(">","&gt;",$unsafe);
$unsafe = str_replace("\"","&quot;",$unsafe);
$unsafe = str_replace("\'","&apos;",$unsafe);
return $unsafe;
}
?>

Omitting <?php and ?> will simple display the contents as text (which isn't what you want) :)
 
Got it all working now a bit of tweaking. I have added a description to show some content. Ulitmatley what I want is for the description to be populated by data from two fields in the database city and address.

Code:
$rt = $row["name"];
		$rl = $row["city"];
		$rd = $row["address"];
		echo "<item>\r\n";
		echo "<title>" . xml_ready($rt) . "</title>\r\n";
		echo "<description>" . xml_ready($rd) .  "</description>\r\n";
		echo "<link>" . xml_ready($rl) . "</link>\r\n";
		echo "</item>\r\n";
How can I get that decription to be filled with $rd and $rl ?
 
Alternatively, just generate the feed dynamically (on request) using the XMLWriter class.

This way, you don't have to worry about writing the XML yourself or sanitising feed content, as PHP's XML libraries take care of this for you. You also don't have to worry about maintaining a separate XML file, which could become corrupted.
 
Last edited:
Hehe I am at the limit of my knowledge as it is I am nearly there with this solution just need to pull some data from the table fields to fill the description, though if you have any examples I would be interested to see it in action.
 
Woot !!

Getting closer I have worked out how to join the variable together using string concatenation but now need to get a space in between each one. Any ideas ?

Code:
$rt = $row["name"];
		$rl = $row["city"];
		$rd = $row["address"];
		$full = $rt.$rl.$rd;
		echo "<item>\r\n";
		echo "<title>" . xml_ready($rt) . "</title>\r\n";
		echo "<description>" . xml_ready($full) .  "</description>\r\n";
		echo "<link>" . xml_ready($rl) . "</link>\r\n";
 
Sussed it putting a space between two quotation marks did the trick :)
Code:
$rt = $row["name"];
		$rl = $row["city"];
		$rd = $row["address"];
		$full = $rt." ".$rl." ".$com." ".$rd;
		echo "<item>\r\n";
		echo "<title>" . xml_ready($rt) . "</title>\r\n";
		echo "<description>" . xml_ready($full) .  "</description>\r\n";
		echo "<link>" . xml_ready($rl) . "</link>\r\n";
		echo "</item>\r\n";
 
Back
Top Bottom