♦View your IP Address: (none)♦Members♦Help♦Bookmark♦NewHOT♦110mb♦Alexa Ranking♦
♦Study♦InfoTech♦Security♦Entertainment♦Tourism♦Career♦Social♦Business♦Science♦
♦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...- Introduction to Server Side Includes (SSI).
- Enable SSI On Your Server via .htaccess file.
- Include Command of SSI.
- Echo Command of SSI.
- Date Command of SSI.
- Config Command of SSI.
- Exec Command of SSI.
- Other Basic Commands of SSI.
- SSI Conditional Basics.
- Conditional Expressions of SSI.
- Choose colors for your website.
- 10 Reasons Why People Don't Visit Your Web Site.
- 10 Profitable Reasons To Add A Discussion Board Or Chat Room To Your Web Site.
- 10 Legal Commandments for Your website.
- A sample for comments include Google Search results on Web, Videos, Blogs.
- Add Favicon to further promote your website.
- Website Design and content, technical guidelines and basic principles.
- List of common MIME types and their corresponding file extensions.
- What is a RSS Feed ? How to make Static Feeds and Dynamic Feeds.
- Resources design website for webmasters.
♦Email to your friend ♦Email to us ♦Get updates by Email ♦View updates on Web ♦Follow us on Twitter ♦Google Safe Browsing ♦McAfee SiteAdvisor ♦Diagnostic this page ♦Noreton SafeWeb ♦DrWeb ♦Technorati Links ♦Save to del.icio.us ♦Add to del.icio.us ♦Digg This! ♦Share on Facebook ♦outside.in: geotag this story ♦Discuss on Newsvine ♦Stumble 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!