View your IP Address: (none)♦MembersHelpBookmarkNewHOT110mbAlexa Ranking

Vietnamese | English

StudyInfoTechSecurityEntertainmentTourismCareerSocialBusinessScience
World Times♦Vietnam:
♦California-USA:

What is a RSS Feed ?



Products / Services

Really Simple Syndication Feeds – are an extension of XML. RSS outputs frequently updated data in a readable format for readers. RSS Feeds are great for blogs, podcasts, news, forums, or anything that is frequently updated. RSS feeds can have the extension .xml or .rss although, later in this tutorial I will show you how to rewrite the URL so that you can display it similarly.

Static Feeds

For a rarely updated feed, you may find it easier to manually update the file rather than have a dynamic feed. First, I will show you a full example of an RSS feed then will explain each tag. Here is a complete RSS Feed source code:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>My RSS Feed</title>
<link>http://www.example.com</link>
<description>description of your feed.</description>
<item><title>News</title>
<link>http://www.example.com/news/News-Story</link>
<description>some news at the site </description>
</item>
<item><title>News</title>
<link>http://www.example.com/news/News-Story-again</link>
<description>some more news</description>
</item>
</channel>
</rss>

There you have your first RSS Feed! Try saving it with the extension ".xml" or ".rss". Upload it to your website or to your local web server and you should see a very basic feed!. View DEMO.

Okay, so you have a feed. But maybe you update your sites' news everyday or you post frequently on your blog. It's a lot of hassle to update your feed each and every time! The solution? Dynamic Feeds!

Dynamic Feeds

Dynamic RSS feeds will update themselves as soon as you post that new blog entry, news headline, or whatever your feed is about! For this we use a mixture of MySQL, PHP and RSS. You wonder how a PHP file can display XML ? It's very simple to do – we just use PHP Headers:

Step 1: Create the .htaccess file

RSS files, like all XML files, are static text. To make our feed dynamic, we’re going to tell our server to treat files with an .xml extension as PHP files.

To do that, simply create a file named .htaccess. Add AddType application/x-httpd-php .xml and save it to your server. If you have access to your server’s configuration (httpd.conf) file, you can put the AddType declaration there instead.

Keep in mind that this will cause all files with an .xml extension in that directory (or on your server) to be parsed as PHP.

Step 2: Set the header

By default, PHP sends text with an HTML content type. As a result, your aggregator may not recognize it as a valid RSS / XML file.

Enter the header() function. We’ll put the following header near the top of our script, before any text gets sent to the client.

<?php header('Content-type: text/xml'); ?>

This header will tell the browser that the file is an XML file, regardless of the extension ".php". Next, we must tell the browser that this is an XML/RSS file again:

Feed For RSS 2.0 standard with charset: ISO-8859-1

<?php 
echo '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">';
?>

Feed For RSS 1.0 standard

<?php
echo '<?xml version="1.0" ?> 
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns="http://purl.org/rss/1.0/" 
xmlns:dc="http://purl.org/dc/elements/1.1/">';
?>

Feed For RSS 2.0 standard with charset: UTF-8

<?php
echo '<?xml version="1.0" encoding="utf-8" ?> 
<?xml-stylesheet type="text/css" ?> 
<rss version="2.0" 
xmlns:dc="http://purl.org/dc/elements/1.1/">';
?>

Feed For ATOM standard

<?php
echo '<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">';
?>

Step 3: The basic feed Begining

A basic RSS document consists of a channel. Within each channel are items. At minimum, each item requires a title, description and link. In this example, we’re also going to add a publication date.

Your feed should start off like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>Name of your site</title>
<description>A description of your site</description>
<link>http://yoururl.com/</link>

Step 4: Retrieve entries from the database

Obviously, everyone's CMS will have a different method of displaying entries. Here is a basic example that may be similar to your CMS.

<?php 
mysql_connect('localhost', 'username', 'password');
mysql_select_db('news');
$query = "select * from entries order by id DESC";
$results = mysql_query ($query) or die (mysql_error());
while ($row = mysql_fetch_assoc($results)) 
    //will loop through all rows
    echo '<item><title>', strip_tags($row['title']), 
    '</title>', "\n",
	'<link> ', $row['link'], '</link>', "\n",
	'<description>', htmlentities(strip_tags(substr(
    $row['entry'], 0, 600))), '..</description>', "\n",	
	'</item>', "\n\n";
?>

First, we connect to the database that contains the entries and select the database we are using. Next, we perform a query that will grab all the rows that contain each entry, ordered so that the newest entries appear first. Now, we loop through the query and put each row into an <item> tag.

We use strip_tags() to remove any HTML tags that may be in the title or description and substr() to display the first 600 characters of the post. Edit this to suit your CMS.

Now that we've closed the PHP tag, we just close the tags that are left open:

</channel>
</rss>

If you put that all together you will get the following:

<?php header('Content-type: text/xml'); 
echo '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">';
mysql_connect('localhost', 'username', 'password');
mysql_select_db('news');
$query = "select * from entries order by id DESC";
$results = mysql_query ($query) or die (mysql_error());
while ($row = mysql_fetch_assoc($results)) 
    //will loop through all rows
    echo '<item><title>', strip_tags($row['title']),
    '</title>', "\n",
	'<link> ', $row['link'], '</link>', "\n",
	'<description>', htmlentities(strip_tags(substr(
    $row['entry'], 0, 600))), '..</description>', "\n",	
	'</item>', "\n\n";
?>
</channel>
</rss>

Edit this to your needs then upload it to your server. Pretty cool, yeah ?

Step 5: Validate and announce

Before you make your feed "live", please don’t forget to check it with a validator. An invalid feed can cause problems with aggregators and browsers. There are several web-based validation tools such as Feed Validator that make validation a snap.

However, if you don't want your visitors to see this file as having a ".php" extension. We can tackle this with apache's mod_rewrite. Create or edit your .htaccess file and put in the following code:

RewriteEngine on
RewriteRule ^feed$ feed.php [L]

Now, if your site were example.com you can open feed.php via www.example.com/feed, which is a lot cleaner and easier on the eyes. You can change "feed" to anything you want. If you do save your PHP as something other than feed.php remember to change the code in the htaccess file, too.

View more... 
Select Vietnamese to find other information or service from Vietnamese site(s).

Email to your friendEmail to usGet updates by EmailView updates on WebFollow us on TwitterGoogle Safe BrowsingMcAfee SiteAdvisorDiagnostic this pageNoreton SafeWebDrWebTechnorati LinksSave to del.icio.usAdd to del.icio.usDigg This!Share on Facebookoutside.in: geotag this storyDiscuss on NewsvineStumble It!Add to Mixx!

If you would like to submit your website for FREE or advertise your own webpage(s) on our webpages, please enter Terms of Service webpage to send your request. Thank you!

©2004- ESI Portal ™®

Products / Services

(\__/)
(=^.^=)
(")_(")
ESI Portal
HOME
Top News
Online Tools
Online Translator
WYSIWYG Editor
Source Code Editor
Real Time Tool
2D Map Tool
3D Map Tool
Forex News
Online Shopping
Watch Videos
Listen Songs
Find Jobs
For Webmasters
About Us
Privacy Policy
Terms of Service
Contact Us
Live chat with us