|
|
Sep 10, PST
|
|
Generating the Skeleton of the Feed Before getting into the PHP part of the code, you must first create the skeleton of the feed, the feed properties, title, description, website link, etc. To make things simple you can start with this. At the end of the article, I will cover more about adding "finishing touches" to your feed. We'll start with this: <?php header('Content-Type: application/xml'); echo'<?xml version="1.0"?>'; ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> <channel> <title>FEED TITLE</title> <link>http://www.yourwebsiteurl.com</link> <description>FEED URL</description> <docs>http://blogs.law.harvard.edu/tech/rss</docs> <generator>FEED GENERATOR(you can put your site here, I guess)</generator> <atom:link href="http://www.yourwebsiteurl.com/feed.php" rel="self" type="application/rss+xml" /> </channel> </rss> It's very important to note here that the <atom:link> tag should have the URL of the feed. It may seem redundant, but there are applications that use this for better efficiency in updating the feed. Adding Items to the Feed Here's where we use PHP to call up the feed content from a MySQL database. First remember to connect to the database at the beginning of the code and to disconnect it at the end of the code. Next, we'll use a simply query to call up the necessary information. You'll modify the database info and the query to match your needs. <?php $dbhost='localhost'; $dbuser='USERNAME'; $dbpass='PASSWORD'; $dbname='DATABASE'; $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to $dbhost"); mysql_select_db($dbname) or die("Cannot select $dbname"); header('Content-Type: application/xml'); echo'<?xml version="1.0"?>'; ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> <channel> <title>FEED TITLE</title> <link>http://www.yourwebsiteurl.com</link> <description>FEED URL</description> <docs>http://blogs.law.harvard.edu/tech/rss</docs> <generator>FEED GENERATOR(you can put your site here, I guess)</generator> <atom:link href="http://www.yourwebsiteurl.com/feed.php" rel="self" type="application/rss+xml" /> <?php $query = "SELECT title, time, author, post, alturl, cat FROM comm_news ORDER BY time DESC LIMIT 0, 10"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo' <item> <title>',htmlspecialchars($row['title']),'</title> <link>',$newsurl,'</link> <description><![CDATA[',$row['post'],']]></description> <pubDate>',date("D, d M Y H:i:s",$row['time']),' -0700</pubDate> </item> '; } ?> </channel> </rss><?php mysql_close($conn); ?> Conclusion - Adding Finishing Touches There you go! Once you alter the specifics, you should get this to generate an RSS Feed for your site. Now if you would like to add some more to your feed or would like to learn what a feed can do, then I highly recommend you go through W3Schools RSS Tutorial. Using the navigation on the left, you can see the different types of tags that are available to you and how you can implement. These are just bells and whistles, the basic feed (above) is really all you need, but it's nice to have a well-developed feed as well.
| Written by: Haily | Added: Mar 26 2010 | Last Modified: Mar 27 2010 | Views: 732 | User Comments
| ||||||||||
|
Enchant Me Not | Unloadeed | Tugboat | Rabidish | Dash of Color | Starlett | NeopetsGuide | Figmint | Neo Crave | Rogue City | Spicy-Sugar | FGN-Guild.com | RainbowBliss | KeliJo.net | NeoWishes |
||||||||||