RSS generating with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lethergic
Forum Newbie
Posts: 4
Joined: Thu Mar 03, 2005 1:09 pm

RSS generating with PHP

Post 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
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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.
lethergic
Forum Newbie
Posts: 4
Joined: Thu Mar 03, 2005 1:09 pm

Post 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... :roll:
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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?
lethergic
Forum Newbie
Posts: 4
Joined: Thu Mar 03, 2005 1:09 pm

Post by lethergic »

table news
fields:
id
subject
message
name
date
email
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Thanks. I will give you a semi-functional example shortly.
lethergic
Forum Newbie
Posts: 4
Joined: Thu Mar 03, 2005 1:09 pm

Post by lethergic »

thx a lot! :D

Greetz Lethergic
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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&#1111;"dc"]="http://purl.org/dc/elements/1.1/";

	/*
	 *  Define the properties of the channel.
	 */
	$properties=array();
	$properties&#1111;"description"]="This is a test channel";
	$properties&#1111;"link"]="http://www.somesite.com/";
	$properties&#1111;"title"]="My channel...";
	$properties&#1111;"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) &#123;
	  $DBLink = mysql_select_db('database');
	  
	  if ($DBLink !== false) &#123;
	    // 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) &#123;
        while ($row = mysql_fetch_assoc($RSSResult)) &#123;
          $properties=array();
          $properties&#1111;"description"]=$row&#1111;'subject'];
          $properties&#1111;"link"]="http://www.somesite.com/pathtosomephpthatshowsthisarticle.php";
          $properties&#1111;"title"]=$row&#1111;'message'];
          $properties&#1111;"dc:date"]="2002-05-06T00:00:00Z";
          $rss_writer_object->additem($properties);
        &#125;
      &#125;
	  &#125;
	&#125;

	
	/*
	 *  When you are done with the definition of the channel items, generate RSS document.
	 */
	if($rss_writer_object->writerss($output))
	&#123;
		
		/*
		 *  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;
	&#125;
	else
	&#123;

		/*
		 *  If there was an error, output it as well.
		 */
		Header("Content-Type: text/plain");
		echo ("Error: ".$rss_writer_object->error);
	&#125;
?>
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Where is your site? I get 404's and a strange looking home page...
Post Reply