什么是RSS?百度百科是这么说的: http://baike.baidu.com/view/1644.htm
原理就是通过一个mysql数据库查询语句,获取最新志信息用于循环输出,并且由php生成xml的格式,然后输出。
代码如下:
require('db_config.php');
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
mysql_query("set names utf8");
$result = mysql_query("select * from `blog` order by `Id` desc limit 0,8");
$blogurl = "http://".$_SERVER["HTTP_HOST"];
header("Content-type:application/xml");
$d_rss = "";
$d_rss.="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$d_rss.="<rss version=\"2.0\">\r\n";
$d_rss.="<channel>\r\n";
$d_rss.="<title>some title - urname's Blog</title>\r\n";
$d_rss.="<link>$blogurl</link>\r\n";
$d_rss.="<description>urname's Blog</description>\r\n";
$d_rss.="<language>zh-cn</language>\r\n";
$d_rss.="<generator>$blogurl</generator>\r\n";
while($row = mysql_fetch_array($result)){
$d_rss.="<item>\r\n<title>".$row["BlogTitle"]."</title>\r\n<link>".$blogurl."/index.php/Blog/details/id/".$row["BlogId"].".html</link>\r\n<description><![CDATA[".strip_tags($row["BlogContent"])."]]></description>\r\n<pubDate>".date('r', strtotime($row['BlogAddTime'])+16*3600)."</pubDate>\r\n</item>\r\n";
}
$d_rss.="</channel>\r\n</rss>";
echo $d_rss;
然后在页面的<head>与</head>标签之间加入如下代码:
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有文章" href="http://www.urdomain.com/rss.php" />
在到页面合适的地方加一个RSS的链接即可。效果如下:
http://www.urdomain.com/rss.php
---------------------------------------------
一个很好用的PHP生成RSS的类:FeedCreator
发现了一个很好用的PHP生成RSS的类:FeedCreator。尝试了一下,很不错,使用也非常简单,不多说,看代码:
<?php
include("feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->useCached(); // use cached version if age<1 hour
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
//optional
$rss->descriptionTruncSize = 500;
$rss->descriptionHtmlSyndicated = true;
$rss->link = "http://www.dailyphp.net/news";
$rss->syndicationURL = "http://www.dailyphp.net/".$_SERVER["PHP_SELF"];
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;
$rss->image = $image;
// get your news items from somewhere, e.g. your database:
mysql_select_db($dbHost, $dbUser, $dbPass);
$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = $data->title;
$item->link = $data->url;
$item->description = $data->short;
//optional
$item->descriptionTruncSize = 500;
$item->descriptionHtmlSyndicated = true;
$item->date = $data->newsdate;
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
?>
$rss->addItem($item);
}
点击此处下载feedcreator.class.php- https://www.box.com/s/m0sd3qe8fcnbgkfqjk7a
注:(1)、该类默认时区为+01:00,如需修改请修改feedcreator.class.php第159行;
(2)该类默认编码为ISO-8859-1,如需修改请修改feedcreator.class.php第159行.