首先做个演示地址吧http://www.yunphp.cn/index.php?m=Index&a=feed.
我为了方便就直接加在首页模块了。创建一个名字为feed的function代码如下
public function feed()
{
header('Content-type: application/xml');
$cid = isset($_GET['cid']) ? intval($_GET['cid']) : '';
$where = $cid ? "and cid = $cid" : '';
$blog = M('posts')->field('id,subject,description,createdate')->where("1 = 1".$where)->limit('0,10')->select();
echo '<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title><![CDATA[YunPHP - 因为相信,所以坚持 - 专注PHP开发]]></title>
<description><![CDATA[YunPHP是以PHP开发技术交流为主的博客,帮助开发者解决在实际项目中经常遇到的PHP问题]]></description>
<link>http://www.yunphp.cn</link>
<language>zh-cn</language>
<generator>www.yunphp.cn</generator>';
foreach ($blog as $v)
{
echo "
<item>
<title>{$v['subject']}</title>
<link>http://www.yunphp.cn/post/{$v['id']}</link>
<description><![CDATA[{$v['description']}]]></description>
<author>米修</author>
<pubDate>{$v['createdate']}</pubDate>
</item>";
}
echo "</channel>";
echo "</rss>";
}
这个主要就是一个xml的输出,thinkphp自带了一个xml的函数 但是我觉得不够好。没有自定义,没有用模板来解析 直接就用echo来输出了 原生态的!然后就可以通过具体的地址就行浏览了!