10.3. Generating the RSS

Really Simple Syndication (RSS) is an easy way to notify others of content changes. RSS feeds are used to publicize new blog posts, news events, new product additions to e-commerce sites and more. Visitors typically will subscribe to a feed you make available and are then able to automatically review updates directly from within their favorite aggregator without having to visit the site in a browser.

For historical reasons there are several different RSS formats. They are all XML-based although they aren't necessarily compatible with one another. I've chosen RSS 2.0 to publicize the new posts.

The root element of the feed is rss. Information about the site is placed in a channel section using title, link, and description elements. Any number of item elements follow the channel section, one for each post you want to publicize, each with title, link, and description elements.

Here is public_files/rss.php:

<?php header('Content-Type: text/xml'), ?>
<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>My Blog</title>
  <link>http://www.example.com/myBlog</link>
  <description>
   My Blog is a place where I write my innermost thoughts and feelings for
   all the world to read.
  </description>
 </channel>
<?php
// include the 5 most recent entries
$query = sprintf('SELECT POST_ID, POST_TITLE, POST_TEXT, ' .
    'UNIX_TIMESTAMP(POST_DATE) AS POST_DATE FROM %sBLOG_POST ' .
    'ORDER BY POST_DATE DESC', DB_TBL_PREFIX);
$result = mysql_query($query, $GLOBALS['DB']);

$count = 0;
while (($row = mysql_fetch_array($result)) && $count++ < 5)
{
    echo '<item>';
    echo '<title>' . htmlspecialchars($row['POST_TITLE']) . '</title>';
    printf('<link>http://www.example.com/myBlog/view.php?m=%d&y=%d</link>',
        date('n', $row['POST_DATE']), date('Y', $row['POST_DATE']));
    echo '<description>' . htmlspecialchars($row['POST_TEXT']) . '</description>';
    echo '</item>';
}
?>
</rss>

I've included only the required elements in my feed, but there are others you can include to provide more information. Table 10-1 shows a listing of all elements for the RSS 2.0 specification.

Table 10-1. RSS Elements
ElementDescriptionExample
rssRoot element.
<rss version="2.0">
 <channel> ... </channel>
</rss>

channelSection that contains information about the feed's originating website and item elements.
<channel>
 <title>...</title>
<link>...</link>
 <description>
 ... </description>
<item> ... </item>
 <item> ... </item>
 ...
</channel>

itemIndividual item entries.
<item>
 <title>...</title>
<link>...</link>
 <description>
 ...
 </description>
</item>

channel Elements  
category(Optional) Specifies one or more site categories.
<category domain="syndic8">
 Web Log</category>

cloud(Optional) Indicates the cloud to be notified of updates.
<cloud domain="www.example.com
"port="80" path="/RPC" protocol="xml-rpc"
 registerProcedure="NotifyMe" />

copyright(Optional) Specifies copyright statement.
<copyright>
 Copyright 2007
</copyright>

description(Optional) Provides web site description/summary.
<description>
 My Blog is a place where I write my
innermost thoughts and feelings for all the world to read.
</description>

docs(Optional) Provides documentation address for the RSS format used.
<docs>
 http://blogs.law.harvard.edu/tech/rss
</docs>

generator(Optional) Determines which program was used to generate the feed.
<generator>PHP</generator>

image(Optional) Specifies an image to associate with the feed. Has required child elements url, title and link.
<image>
 <url>
http://www.example.com/img/logo.png
 </url>
 <title>My Blog</title>
<link>http://www.example.com</link>
</image>

language(Optional) Indicates in which language the feed is written.
<language>en-us</language>

lastBuildDate(Optional) Indicates RFC 822-formatted date the feed was last modified.
<pubDate>
Wed, 21 Nov 2007 21:55:00 EST
</pubDate>

linkSpecifies web site address.
<link>http://www.example.com</link>

managingEditor(Optional) Specifies e-mail address of the feed's managing editor.
<managingEditor>
 [email protected]
</managingEditor>

pubDate(Optional) Provides RFC 822-formatted date the feed was published.
<pubDate>
 Wed, 21 Nov 2007 21:55:00 EST
</pubDate>

rating(Optional) Shows Platform for Internet Content Selection (PICS) rating for the feed.
<rating>
 (PICS-1.1 "http://www.classify.org/
safesurf/" L  gen true for "http://www
.example.com"  r (SS~~000 6 SS~~001 2
SS~~002 2 SS~~003 2 SS~~004 2 SS~~005 1
SS~~009 1))
</rating>

skipDays(Optional) Specifies which days the RSS aggregator should skip updating the feed
<skipDays>Saturday</skipDays>

<skipHours>(Optional) Specifies which hours the RSS aggregator should skip updating the feed (24-hour, 0 represents midnight).
<skipHours>0</skipHours>

titleSpecifies the name of web site.
<title>My Blog</title>

ttl(Optional) Specifies cache time to live in minutes.
<ttl>120</ttl>

webMaster(Optional) Specifies e-mail address of the site's webmaster.
<webMaster>
 [email protected]
</webMaster>

item Elements  
author(Optional) Specifies e-mail address of the item's author.
<author>
 [email protected]
</author>

category(Optional) Specifies one or more item categories.
<category domain="syndic8">
 Web Log</category>

comments(Optional) Provides link to comments about the item.
<comments>
http://www.example.com/comments.php
</comments>

descriptionSpecifies item description/summary.
<description>
 Once upon a time...
</description>

enclosure(Optional) Links a media file to the item. Has required child elements length, type and url.
<enclosure url="http://www.example.com/
vid.mov" length="659435" type="video/
quicktime" />

guid(Optional) Provides a Globally Unique Identifier for the item.
<guid>
http://www.example.com/item123456
</guid>

linkLinks to item.
<link>
http://www.example.com/view.php?m=11&
y=2007
</link>

pubDate(Optional) Indicates RFC 822-formatted date the item was published.
<pubDate>
 Wed, 21 Nov 2007 21:55:00 EST
</pubDate>

source(Optional) Provides third-party source for item.
<source url="http://example.org">
example.org</source>

titleIndicates the name of the item.
<title>
 My First Blog Entry
</title>


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.155.130