Saturday, January 17, 2009

বাংলা জিমেইল ক্লিপ ‌: ২ / Enable your site for Gmail Clips - Tutorial

Gmail Clip requires a valid RSS feed as described below. You can easily make one RSS feed with few lines of PHP code. If you dont want to read much  download following code, save as rss.php  and modify over that. 

Step 1: Get your basic RSS structure.


RSS is an XML-based format, so all you really need is a set of XML tags as part of the RSS specification. RSS provides a number of optional tags, and being XML, it can be (and often is) extended with any other tag. 




<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>My Blog Name</title>
<link>http://example.com/blog</link>

<description>Keeping webmasters up-to-date on technology.</description>
<pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
<item>
<title>Story Title</title>

<pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
<link>http://example.com/my/story</link>
<description>Today I saw a whale!</description>
</item>

</channel>
</rss>


One important thing is encoding. It must be utf-8 otherwise Unicode bangla will be broken. The easiest way to build your RSS feed in PHP is to leave all of that XML as plain text and only loop over the sections as needed (the <item> tags). The one gotcha here, as you’ve probably noticed, is the opening <?xml tag. This will probably clash with PHP, so instead echo that line out:




<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>



Then your structure is all in place. The item element is repeated for each story we have in our RSS feed; the date is RFC 2822 formatted (just use date("r")). We’ll use a simple foreach loop to put the content in:




<?php foreach ($items as $item) { ?>

<item>
<title><?=$item['title']?></title>
<pubDate><?=date("r",$item['time'])?></pubDate>
<link><?=$item['url']?></link>

<description><?=$item['description']?></description>
</item>
<?php } ?>


2. Serve it up!

To be recognised as an RSS feed, you’ll need to serve it as the right content type. Technically you should use application/rss+xml, but most browsers won’t recognise this. I find application/xhtml+xml works well, as does text/xml. The choice is yours, although IE7 seems to like text/xml better.





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


And we’re done!


Here’s a sample of the final product:




<?php header("Content-type: text/xml"); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>

<rss version="2.0">
<channel>
<title>My Blog Name</title>
<link>http://example.com/blog</link>
<description>Keeping webmasters up-to-date on technology.</description>

<pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
<?php foreach ($items as $item) { ?>
<item>
<title><?=$item['title']?></title>
<pubDate><?=date("r",$item['time'])?></pubDate>

<link><?=$item['url']?></link>
<description><?=$item['description']?></description>
</item>
<?php } ?>
</channel>

</rss>

No comments:

Post a Comment