当前位置:首页 > WordPress博客 > 使用PHP来读取RSS的方法(可用在WP主题中读自己其他博客的RSS喔)

使用PHP来读取RSS的方法(可用在WP主题中读自己其他博客的RSS喔)

发布时间: 2012-01-09 暂时没有回复 关键词 : ,

 

几乎每一个博客都有自己的RSS聚合系统,针对RSS,以下这个小程序,实现了与BLOG的同步调用。在WordPress的functions.php中添加以下函数,不是WP程序的话就加在能引用到的文件中即可:

if ( ! function_exists(‘get_feed’)){
function get_feed($rss=null){
$time_to_update=43200; //设定12小时更新一次
$opt=”;
$source=$rss;
if (empty($source)) $source=’http://lizus.com/feed’;//此处设定默认情况下的RSS源
$buff =’./cache/feed/’.sha1($source).’.xml’;//此处设定Cache存放位置
$last_modified=filemtime($buff);
$now=time();
$need_update=0;
if (!empty($last_modified) && ($time_to_update-($now-$last_modified))<1){
$need_update=1;
}
if( !file_exists($buff) || $need_update ) {
$str = @file($source) or die(‘加载文件时出错。’);
$str = join(”,$str);
$fp = fopen($buff,’w') or die(‘写缓存失败!’);
fputs($fp,$str);
fclose($fp);
}
if (file_exists($buff)) {
$xml = simplexml_load_file($buff);
$opt = &$xml->channel;
}
return $opt;
}
}

然后在需要调用其他站点RSS的位置上添加如下代码:

<?php $rs=get_feed();
foreach ($rs->item as $v){
echo ‘<li><a href=”‘.$v->link.’” title=”‘.$v->title.’”>’.$v->title.’</a> <span>’.date(‘Y/m/d’,strtotime($v->pubDate)).’</span></li>’;
}

?>

这样就可以看到调用的RSS了,至于要调用其他内容,则可以查看相应的RSS输出文档,类似操作即可.

 

发表评论

电子邮件地址不会被公开。

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!:

go top