2009年的第一天,我在公司度过。
php对于xml和json的支持非常强大,下面要提到的阅读器就是基于simplexml与json来完成的。
整个过程采用异步加载:
jquery访问提供rss数据的接口文件,然后分析数据并返回结果。
demo :
http://yibin.us/phpdemo/home.php
首先要了解simplexml类的相关方法,这里只需用到simplexml_load_file加载一个rss就行了。
简易rss类,该类用来加载一个rss
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of rssclass
*
* @author yibin
*/
class rss {
private $rssurl;
private $info = array();
private $items = array();
function RssUrl()
{
return $this->rssurl;
}
function GetItems()
{
return $this->items;
}
function GetRssInfo()
{
return $this->info;
}
function Load()
{
if(!empty($this->rssurl))
{
$channel = simplexml_load_file($this->rssurl);
$this->info['title'] = $channel->channel->title;
$this->info['link'] = $channel->channel->link;
foreach($channel->channel->item as $item)
{
$this->items[] = $item;
}
}
}
function __construct($rss='')
{
$this->rssurl = $rss;
}
}
?>
服务接口:
<?php require "rss.cache.php"; $url = $_GET['url']; $rss = new RssProvider(); $item = $rss->LoadRss($url); echo json_encode($item); //将数据转成json形式。 ?>
前台的js将访问上面的服务接口。
js部分:
//url为rss地址
//obj为HTML容器ID。
function getContent(url,obj){
url = 'clib/rss.service.php?url='+url;
$.getJSON(url,function(json){
var info = json.info;
var items = json.items;
var s='<dl><dt><a href="'+info.link[0]+'" target="_blank">'+info.title[0]+'</a></dt>';
for(var i = 0;i<items.length;i++)
{
s+='<dd><em>'+(i+1)+'</em><a href="'+items[i].link+'" target="_blank">'+items[i].title+'</a></dd>';
}
s+='</dl>';
$('#'+obj).html(s);
});
}
html部分:
<div id="main">
<div class="news" id="news_1">
数据加载中......
</div>
<div class="news" id="news_2">
数据加载中......
</div>
<div class="news" id="news_3">
数据加载中......
</div>
<div class="news" id="news_4">
数据加载中......
</div>
</div>
<script>
getContent('<?php echo urlencode('http://news.google.cn/nwshp?tab=wn&ned=ccn&topic=e&output=rss')?>','news_1');
getContent('<?php echo urlencode('http://news.google.cn/nwshp?tab=wn&ned=ccn&topic=y&output=rss')?>','news_2');
getContent('<?php echo urlencode('http://feed.feedsky.com/yibin')?>','news_3');
getContent('<?php echo urlencode('http://www.iceapple.net/syndication.axd')?>','news_4');
</script>
不错,用过IXNA 最新版问题很多..
没提供打包下载~ :p
我下一阶段也要用到,先学习一下!
有现成的开源类 lastRSS
强啊,学几天就能做出这个东西了。^_^
@peter:
因为巨简单,所以当时未提供。
更新了日志,提供了下载。
IXNA没时间更新,这种simplexml的生成是最快,但生成<RSS></RSS><RSS></RSS>的话,好像只能生成第一个,后面的出不来