I've created these two PHP scripts for outputting RSS from the data in your News Aggregator. One is for RSS 1.0, the other for RSS 2.0. They both work great, but if someone can suggest a way to include a published date, it would help. It seems that the news aggregator uses a timestamp function that doesn't translate into a good pubdate. When I place the feeds into MyYahoo, it says there are no new feeds (within the last 3 days). Feed free to play with these and mark them up as you see fit.

What you will need to change in the scripts are:

RSS 1.0 Script:
"title" "description," "link," and "items" information.
Database "username," "password," and "database name."

RSS 2.0 Script:
"title," "link," "description," and "language" (if other than English).
I've included a badge for my site - and feel free to leave it there :) - but if you want to use your own, change the image "url," "link," "title," "height," and "width" information. You can also leave this information blank between the tags.
Database "username," "password," and "database name."

Save the script of your choice as "rss.php" and upload it to the main directory. I went into CPanel and redirected "index.rdf," "index.xml," and "rss.xml" to this page, since this is what search engines crawl for when looking for an RSS file.

Here are the scripts, and like I said, please offer any suggestions for fixes or better usability:

RSS 1.0

<?php

/**********************************************************************************
 * rss.php
 * Version: 1.00
 * Author: Jeffrey Smith
 * Date: 03/19/2001
 * http://www.bloggator.com
 *
 **********************************************************************************
 This program is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE.

 This work is hereby released into the Public Domain. To view a copy of the public
 domain dedication, visit http://creativecommons.org/licenses/publicdomain/ or
 send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
 94305, USA.
 **********************************************************************************/
//create header information and call the database

header ("");

echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
?>
<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:hr="http://www.w3.org/2000/08/w3c-synd/#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/">
    <channel rdf:about="http://www.bloggator.com/rss.php">
        <title>Put Title Here</title>
        <description>Put Description Here</description>
        <link>Put Link Here</link>
        <items>Put Item Description Here</items>
</channel>

<?php
mysql_connect ("localhost", "username", "password") or die ("Cannot
connect to database server.");
mysql_select_db ("database name") or die ("Cannot connect to database.");


// change the sql look up so that you can format the date correctly// 
// $result = mysql_query ("select * from aggregator_item order by iid desc limit 0,10") or die (mysql_error());
$myq = 'SELECT iid, title, link, description, DATE_FORMAT( timestamp, \'%Y-%m-%d\' ) AS mydate'
        . ' FROM aggregator_item'
        . ' ORDER BY timestamp DESC '
        . ' LIMIT 0 , 15';
$result = mysql_query($myq) or die (mysql_error());



// change the item to items as directed by the RSS Validator// 

while ($row = mysql_fetch_array ($result)) {
    echo ("<item rdf:about><title>");
    echo $row['title'];
    echo ("</title>
        <description>");
    echo $row['description'];
    echo ("</description><link>");
    echo $row['link'];
    echo ("</link><date>");
    echo $row['timestamp'];
    echo ("</date>");    
    echo ("</item>\n\n");
}

mysql_free_result ($result);

?></rdf:RDF>

RSS 2.0

<?php

/**********************************************************************************
 * rss.php
 * Version: 2.00
 * Author: Jeffrey Smith
 * Date: 03/19/2001
 * http://www.bloggator.com
 *
 **********************************************************************************
 This program is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE.

 This work is hereby released into the Public Domain. To view a copy of the public
 domain dedication, visit http://creativecommons.org/licenses/publicdomain/ or
 send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
 94305, USA.
 **********************************************************************************/
//create header information and call the database
header ("");

echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
?>
<rss version="2.0"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:itms="Website Address">
    <channel>
        <title>Put Title Here</title>
        <link>Put your URL Here</link>
        <description>Put Description Here</description>
        <language>en</language>
        <ttl>60</ttl>
		<dc:creator>Bloggator.com</dc:creator>
		<dc:date>2002-10-02T10:00:00-05:00</dc:date>

		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
		<sy:updateBase>2003-09-01T12:00+00:00</sy:updateBase>
		<image>
			<url>http://www.bloggator.com/images/bloggator_badges/bloggator_88x31.gif</url>
			<link>http://www.bloggator.com</link>
			<title>Bloggator.com</title>
			<height>31</height>
			<width>88</width>
		</image>

<?php
mysql_connect ("localhost", "username", "password") or die ("Cannot
connect to database server.");
mysql_select_db ("database name") or die ("Cannot connect to database.");


// change the sql look up so that you can format the date correctly// 
// $result = mysql_query ("select * from aggregator_item order by iid desc limit 0,10") or die (mysql_error());
$myq = 'SELECT iid, title, link, description, DATE_FORMAT( timestamp, \'%Y-%m-%d\' ) AS mydate'
        . ' FROM aggregator_item'
        . ' ORDER BY timestamp DESC '
        . ' LIMIT 0 , 15';
$result = mysql_query($myq) or die (mysql_error());



// change the item to items as directed by the RSS Validator// 

while ($row = mysql_fetch_array ($result)) {
    echo ("<item><title>");
    echo $row['title'];
    echo ("</title>
        <description>");
    echo $row['description'];
    echo ("</description><link>");
    echo $row['link'];
    echo ("</link><date>");
    echo $row['timestamp'];
    echo ("</date>");    
    echo ("</item>\n\n");
}

mysql_free_result ($result);

?></channel></rss>

Comments

bloggator’s picture

Actually, they're working fine now in My Yahoo, it must have been a cache issue with Yahoo. I'm using the RSS 2.0 on my site.

Jeff
Bloggator.com
Get your blog on.

Cloud’s picture

As a semantic web person, I'm more interested in the RSS 1.0 version. It would be good to add some DC (dc:subject) and FOAF data there too...

Edit: My mistake, I thought this was for syndicating Drupal content.

er_lion’s picture

this is a great script, but for working I had to change:

header ("");
echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
?>

to:

Header( "Content-type: text/xml");
echo '<'; ?>?xml version="1.0" encoding="ISO-8859-1"?>

I hope this will help others

thecod’s picture

That's what I needed to make it work. I don't know php, rss, xml, etc, so easy cut paste and voila! works.

Perfecto!

nibaq’s picture

I changed it to this:

Header( "Content-type: text/xml");
echo '<'; ?>?xml version="1.0" encoding="UTF-8"?>

This way it will render arabic text correctly.

dries’s picture

FYI, Drupal 4.7 will have this built-in.

bloggator’s picture

Fantastic! Will each category have its own feed?

Jeff
Read blogs at Bloggator.com

Uwe Hermann’s picture

thecod’s picture

The xml is failing (if I'm using the right wording...) if a feed item has the '&' symbol. If someone has a title like "this & that", it bombs out.

Doing basic search I see that the '&' symbols need to be "escaped". Can that be put into this code?

I see that this feature (rss feed of aggregator) will be in 4.7... but in the mean time it would be nice to get this working in the mean time.

thecod’s picture

I took this section of the RSS 2.0 feed code:

 echo ("<item><title>");
    echo $row['title'];
    echo ("</title><description>");
    echo $row['description'];
    echo ("</description><link>");
    echo $row['link'];
    echo ("</link><date>");
    echo $row['timestamp'];
    echo ("</date>");
    echo ("</item>\n\n");

And modified to this:

    echo ("<item><title>");
    echo htmlspecialchars($row['title']);
    echo ("</title><description>");
    echo htmlspecialchars($row['description']);
    echo ("</description>");
    echo ("<link>");
    echo $row['link'];
    echo ("</link>");
    echo $row['timestamp'];
    echo ("</item>\n\n");

While it makes something pretty turn into ugly html code, it keeps the RSS feed validated when various blog writers use ampersands. I never realized just how much writers did until the feed broke every day.

To make it easier for me to understand, I did break apart some of the lines. I'm not a php person... not even close.

I also removed the date item. I'm not sure why, but it was always popping as bad ( and empty...? ) on the feed validators.

I look forward to version 4.7, but fear the upgrade....

skeddy’s picture

I was using FeedDemon to aggregate the feed, and the dates were not coming through correctly.

I made the following changes for each item in the feed:
From:

    echo ("<item><title>");
    echo $row['title'];
    echo ("</title>
		<description>");
    echo strip_tags(substr($row['news'],0,300));
    echo ("</description><link>http://skeddy.net/main.php?content=post&amp;id=");
    echo $row['id'];
    echo ("</link><date>");
    echo $row['date'];
    echo ("</date>");
    echo ("</item>\n\n");

To:

    echo ("<item><title>");
    echo $row['title'];
    echo ("</title>
		<description>");
    echo strip_tags(substr($row['news'],0,300));
    echo ("</description><link>http://skeddy.net/main.php?content=post&amp;id=");
    echo $row['id'];
    echo ("</link><pubDate>");
    echo $utc = date("r", $row[date]);
    echo ("</pubDate>");
    echo ("</item>\n\n");

This meant that when the feed was going through to FeedDemon, it was sorting the items on the feed correctly into date order.

All in all, a fantatic script, and I'm very pleased to have found it !

________________
http://skeddy.net