Page 1 of 1
RSS generating with PHP
Posted: Thu Mar 03, 2005 1:14 pm
by lethergic
I want to make rss-feeds for my website. I have a news-system and i want to import the last 10 items in a rss-file. The items are saved in a mysqldatabase. How can i import this items in an xml-file?
Greetz Lethergic
Posted: Thu Mar 03, 2005 1:19 pm
by smpdawg
You may want to look at the phpclasses website. I am sure someone has written a class that would help you with this task.
Posted: Thu Mar 03, 2005 1:26 pm
by lethergic
yes i found a class here:
http://phpclasses.syrned.com/browse/package/560.html
But i don't now how i must use them. I want to take the records from my database and import it in a xml-file. But i don't think that this class can do this...

Posted: Thu Mar 03, 2005 1:28 pm
by smpdawg
Can you show me the structure(s) of the table(s) that have the news that you want to generate a feed from?
Posted: Thu Mar 03, 2005 1:38 pm
by lethergic
table news
fields:
id
subject
message
name
date
email
Posted: Thu Mar 03, 2005 1:45 pm
by smpdawg
Thanks. I will give you a semi-functional example shortly.
Posted: Thu Mar 03, 2005 1:46 pm
by lethergic
thx a lot!
Greetz Lethergic
Posted: Thu Mar 03, 2005 2:08 pm
by smpdawg
Code: Select all
<?php
// Notes by smpdawg:
// 1. I don't have your database or tables so I can only speculate on whether that code is OK.
// 2. Since I do not have your db, I haven't tested ANY of this code.
// 3. You need to supply your MySQL username, password and DB down below before you test.
// 4. Replace somesite.com with your URL.
// 5. Fix the viewing link in the foreach so it can be used to link back to your site.
// 6. Before you go live, you need to have this script spit out RSS friendly dates based on
// your DB.
// 7. Disclaimer, disclaimer, disclaimer. Didn't test it. Don't know that it works. Etc.
// But I will lend assistence if there are problems.
require("xmlwriterclass.php");
require("rss_writer_class.php");
/*
* First create an object of the class.
*/
$rss_writer_object=new rss_writer_class;
/*
* Choose the version of specification that the generated RSS document should conform.
*/
$rss_writer_object->specification="1.0";
/*
* Specify the URL where the RSS document will be made available.
*/
$rss_writer_object->about="http://www.somesite.com/channels.xml";
/*
* Specify the URL of an optional XSL stylesheet.
* This lets the document be rendered automatically in XML capable browsers
* such as Internet Explore 5, Mozilla 5/Netscape 6 or better.
*/
$rss_writer_object->stylesheet="http://www.somesite.com/rss2html.xsl";
/*
* When generating RSS version 1.0, you may declare additional
* namespaces that enable the use of more property tags defined
* by extension modules of the RSS specification.
*/
$rss_writer_object->rssnamespacesї"dc"]="http://purl.org/dc/elements/1.1/";
/*
* Define the properties of the channel.
*/
$properties=array();
$propertiesї"description"]="This is a test channel";
$propertiesї"link"]="http://www.somesite.com/";
$propertiesї"title"]="My channel...";
$propertiesї"dc:date"]="2002-05-06T00:00:00Z";
$rss_writer_object->addchannel($properties);
/*
* Then add your channel items one by one.
*/
$DBConnection = mysql_connect('localhost', 'username', 'password');
if ($DBConnection !== false) {
$DBLink = mysql_select_db('database');
if ($DBLink !== false) {
// Query gets ten rows from news. They will be the last 10 rows in the table.
$RSSQuery = "SELECT * FROM news ORDER BY id DESC LIMIT 10";
$RSSResult = mysql_query($RSSQuery);
if ($RSSResult !== false) {
while ($row = mysql_fetch_assoc($RSSResult)) {
$properties=array();
$propertiesї"description"]=$rowї'subject'];
$propertiesї"link"]="http://www.somesite.com/pathtosomephpthatshowsthisarticle.php";
$propertiesї"title"]=$rowї'message'];
$propertiesї"dc:date"]="2002-05-06T00:00:00Z";
$rss_writer_object->additem($properties);
}
}
}
}
/*
* When you are done with the definition of the channel items, generate RSS document.
*/
if($rss_writer_object->writerss($output))
{
/*
* If the document was generated successfully, you may not output it.
*/
Header("Content-Type: text/xml; charset="".$rss_writer_object->outputencoding.""");
Header("Content-Length: ".strval(strlen($output)));
echo $output;
}
else
{
/*
* If there was an error, output it as well.
*/
Header("Content-Type: text/plain");
echo ("Error: ".$rss_writer_object->error);
}
?>
Posted: Thu Mar 03, 2005 2:54 pm
by timvw
Posted: Thu Mar 03, 2005 2:56 pm
by smpdawg
Where is your site? I get 404's and a strange looking home page...