Adding an RSS or XML Feed to Your Website
I didn't know if I really needed this for my website. I don't use them personally, but the search engines appear to use them for indexing purposes. I also thought I would just learn something new and offer one more feature to perhaps one ad clicking web surfer. Since my time so far has been worth about $0.25/hour to build this site, what is another hour.
What is an RSS Feed?
Basically it is an XML file on a website that contains summary information and links for the website. It allows someone to put it into an RSS news reader program and see your summary results lumped in with other sites' summary information. Readers can skim the RSS information without having to actually visit your site. It is more of a convenience thing than anything.
How do you create one?
It has a set format. You can search google for RSS information and tutorials and there will tons of sites that pop up. If your site doesn't change very often, it might be best to just do it by hand and put on your site.
Making the RSS File Dynamic
The biggest problem with creating these files, is if your site changes frequently (which is kind of the purpose of them). How do you make it dynamic so that it changes when the content changes? I found some code on the internet and modified it for myself and ended up with a short little function that takes care of it. It is written in PHP, but I have posted it below.
# open a file pointer to an RSS file
$fp = fopen ("rss.xml", "w");
# Now write the header information
fwrite ($fp, "nnn");
fwrite ($fp, "Thoughts From My Life n");
fwrite ($fp, "http://thoughtsfrommylife.com/n");
fwrite ($fp, "Advice From Someone Who Doesn't Know Everything n");
fwrite ($fp, "en-us n");
fwrite ($fp, "http://thoughtsfrommylife.com/rss.xml nn");
#Loop through the articles. My query returned the most recent 10
while($row = mysql_fetch_array($result))
{
$articleid = $row["articleid"];
$title = $row["title"];
$content = $row["content"];
$summary = substr($content,0,250);
$description = strip_tags($summary);
#Create the link
$pagelink = "http://thoughtsfrommylife.com/article.php?articleid=$articleid";
#Write out the XML tags for the next item
fwrite ($fp, "- n");
#Add a ... if the article is logner than 250
if (strlen($content) > 250)
{
$description = $description . "....";
}
fwrite ($fp, "
$title n");
fwrite ($fp, "$description n");
fwrite ($fp, "$pagelinkn");
fwrite ($fp, " nn");
}
#Close out the xml syntax and then the file
fwrite ($fp, " n n");
fclose ($fp);
#Create another copy as an .rss file
copy("rss.xml", "rssfeed.rss");
And that is my code segment right there. I added this code to the area where I publish one of my articles. This way it will go in and set update the rss feed each time.
What's Next
I need to add more information to the XML file. You can add a timestamp so I will do that next.
If you enjoyed this post, then make sure you subscribe to my RSS feed or subscribe for email updates. Only one email a day and only if there was a new post.
Related Posts
FeedBurner Stats Have Recovered
Full RSS Feed on Thoughts From My Life
I Have RSS Readers...Hooray
Number of RSS Readers Dropped
Redirect Your RSS Feed To FeedBurner
Category: Computers
0 Comments
No approved comments yet.