|
Posted by NC on 05/03/06 20:04
Pugi! wrote:
> I once saw an article about how to put publicly available rss-feeds on your
> own website, to display a certain number of headlines. But i don't find it
> anymore. Can someone point me towards that or a similar article (some
> keywords would be nice too).
It's simpler, really (in fact, RSS stands for "Really Simple
Syndication"). An RSS feed is an XML file that looks sort of like
this:
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://my.netscape.com/rdf/simple/0.9/">
<channel>
<title>My RSS Feed</title>
<link>http://www.example.com/</link>
<description>My Very Cool Web Site</description>
</channel>
<image>
<title>My Logo</title>
<url>http://www.example.com/images/logo.jpg</url>
<link>http://www.example.com/</link>
</image>
<item>
<title>Most Recent Article</title>
<link>http://www.example.com/article.php?id=555</link>
</item>
<item>
<title>Second Most Recent Article</title>
<link>http://www.example.com/article.php?id=554</link>
</item>
<item>
<title>Third Most Recent Article</title>
<link>http://www.example.com/article.php?id=553</link>
</item>
[More <item> entities...]
</rdf:RDF>
You can have a static *.rss file or a PHP script that would generate
the feed on demand. In the latter case, you can do something like
this:
<?php
header('Content-Type: text/xml');
?><?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://my.netscape.com/rdf/simple/0.9/">
<channel>
<title>My RSS Feed</title>
<link>http://www.example.com/</link>
<description>My Very Cool Web Site</description>
</channel>
<image>
<title>My Logo</title>
<url>http://www.example.com/images/logo.jpg</url>
<link>http://www.example.com/</link>
</image>
<?php
// Connect to DB server and select a database...
$query = 'SELECT id, title FROM articles ORDER BY id DESC LIMIT 10';
$result = mysql_query($query);
while ($record = mysql_fetch_array($result)) {
echo <<<EOR
<item>
<title>{$record['title']}</title>
<link>http://www.example.com/article.php?id={$record['id']}</link>
</item>
EOR;
}
?>
</rdf:RDF>
That's it, really... You also may want to think about sending out a
"Last-Modified:" header with the date/time matching that of the most
recent article; this may help you decrease server load through
caching...
Creating a static file is not much different. Just output the same
content (except HTTP headers, of course) into an *.rss file every time
you add an article to your site...
Cheers,
NC
[Back to original message]
|