In this time, i’d like to show you how to make a RSS enabled site. Firstly, we have to clear things about RSS itself.
RSS: How the content flies everywhere
RSS (Really Simple Syndication, or elses) technology enables content to read not only where it located. How can it be? simple. RSS just a bunch of text file which standardized in formatting. Because of W3C (WWW Consortium) has been pointing XML as a universal data formatting to be exchanged among applications, XML is becoming the foundation of RSS itself.
The Structure of RSS File
RSS file contains some standard tags to be used. Look a sample below:
<?xml version=”1.0″ encoding=”utf-8″?>
<rss version=”2.0″>
<channel>
<title>JCO Donuts and Coffee News</title>
<description>You can syndicate our content freely</description>
<language>en-us</language>
<docs>http://backend.userland.com/rss</docs>
<generator>PHP/</generator>
<item>
<title>Technology Update</title>
<link>http://localhost/jco_ok/index2.php?page=read_news&id=10</link>
<description>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tatio</description>
<pubDate>Thu, 08 Mar 2007 10:14:55 +0700</pubDate>
<guid isPermaLink=”false”>http://localhost/jco_ok/index2.php?page=read_news&id=10</guid>
</item>
<item>
<title>Kongkalikong Jeger</title>
<link>http://localhost/jco_ok/index2.php?page=read_news&id=9</link>
<description>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tatio</description>
<pubDate>Thu, 08 Mar 2007 09:44:42 +0700</pubDate>
<guid isPermaLink=”false”>http://localhost/jco_ok/index2.php?page=read_news&id=9</guid>
</item>
</channel>
</rss>
Detailed explanation:
<?xml version=”1.0″ encoding=”utf-8″?>
The first line XML above means the file must be consider as a XML entity (technically, force browser or any content readers read the file as XML file ).
<rss version=”2.0″>
The second line states itself as RSS 2.0, means, the readers must be able to adopt RSS 2.0 (newest version).
<channel>
The channel opening tag is notice the reader to start fetching the whole body of content.
<title>JCO Donuts and Coffee News</title>
<description>You can syndicate our content freely</description>
<language>en-us</language>
<docs>http://backend.userland.com/rss</docs>
<generator>PHP/</generator>
Above part commonly called as meta of RSS containing the title of feed publisher, description and what language that used and how to get the documentation of RSS technology. Also, what programming language used to output the file.
<item>
<title>Technology Update</title>
<link>http://localhost/jco_ok/index2.php?page=read_news&id=10</link>
<description>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tatio</description>
<pubDate>Thu, 08 Mar 2007 10:14:55 +0700</pubDate>
<guid isPermaLink=”false”>http://localhost/jco_ok/index2.php?page=read_news&id=10</guid>
</item>
Here we go, we are in the most important of the feed. Firstly, browse above bunch of code. The most top line is <item> and the lowest one is </item>. Those tags mark the start and end of data which will be displayed on the reader. The amount of item is simply similiar of how many this part is repeated, ofcourse with a different data inside.
Ok, let’s make an analogy with a news in an ordinary newspaper. Text between <title></title> is relating to news title. <link></link> is similiar with the page you should go for the full text of news. <description></description> means the body of news. <pubDate></pubDate> means when the news is published. <guid></guid> means a string that uniquely identifies the item itself, some kind of indexing. Permalink, common term in blogging context, is a URL that points to a specific entry even it has passed from the front page. Permalink is a portmanteau word made from permanent link and is often simply stated so as to be human-readable.
</channel>
</rss>
Absolutely, the closing tags and define the end of RSS statement. Remember, you must make the RSS file in well-formatted condition or your content will not readable! Go to http://validator.w3.org/ to check your file validity.
Ok, Feed Doesn’t Always a *.xml File
Feed also can be a PHP file, means? It means you can output dynamic data without involving physically file writing mechanism which eat the server resources. Here is the sample script (you will need a MySQL server and database):
<?php
include(“config/config.php”);
include(“lib/class/class_database.php”);
$db = new Database($db['host'], $db['user'], $db['pwd'], $db['name']);
$db->Connect();
$q = “SELECT * FROM news WHERE active = 1 ORDER BY idnews DESC”
$q = new Query($q);
$q->Fetch();
/**********************************************************************************
* rss.php
* Version: 1.00
* Author: Gins
* Date: 05/21/2004
* http://gin2.info/
*
**********************************************************************************
header(“Content-Type: text/xml;charset=utf-8″);
// prepare HTML text for use as UTF-8 character data in XML
function cleanText($intext) {
return utf8_encode(
htmlspecialchars(
stripslashes($intext)));
}
ECHO <<<END
<?xml version=”1.0″ encoding=”utf-8″?>
<rss version=”2.0″>
<channel>
<title>JCO Donuts and Coffee News</title>
<link>http://localhost/jco_ok/feed.php</link>
<description>You can syndicate our content freely</description>
<language>en-us</language>
<docs>http://backend.userland.com/rss</docs>
<generator>PHP/$phpversion</generator>
END;
// loop through the array pulling database fields for each item
do {
//$title = date(“r”, strtotime($q->Fetch["date"]));
$title = cleanText($q->Fetch["title"]);
$image = $site_url.”module/mod_news/image/”.$q->Fetch["image"];
$link = $site_url.”index2.php?page=read_news&id=”.$q->Fetch["idnews"];
$guid = $site_url.”index2.php?page=read_news&id=”.$q->Fetch["idnews"];
$description = cleanText($q->Fetch["body"]);
if(strlen($description) > 200){
$description = substr($description, 0, 200);
$description .= ” <a href=$guid>[more]</a>”
}
$pubDate = date(“r”, strtotime($q->Fetch["date"]));
// display an item
ECHO <<<END
<item>
<title>$title</title>
<image>$image</image>
<link>$link</link>
<description>$description</description>
<pubDate>$pubDate</pubDate>
<guid isPermaLink=”false”>$guid</guid>
</item>
END;
} while ($q->Fetch());
ECHO <<<END
</channel>
</rss>
END;
?>
You will need this SQL script:
CREATE TABLE news (
idnews INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
idnews_category INTEGER(10) UNSIGNED NOT NULL,
iduser INTEGER(10) UNSIGNED NOT NULL,
date DATETIME NOT NULL,
title VARCHAR(200) NOT NULL,
body TEXT NOT NULL,
image VARCHAR(255) NULL,
active TINYINT(1) NOT NULL,
PRIMARY KEY(idnews)
);
Inform Firefox to show RSS icon at Address Bar
After all creating process is done let’s go the next step. How can visitors know that a web page contain a feed? One of solutions from Firefox is showing a RSS icon at the adress bar. You can order Firefox to this by inserting below code between <head></head> of your index file. I.e:
<head>
….
<LINK REL=”alternate” TITLE=”JCO Donut & Coffee | RSS” HREF=”feed.php” TYPE=”application/rss+xml” />
…
</head>
Good luck!
Recent Comments