用PHP读取RSS订阅

PHP编程技术 网络投稿 2021-12-22 17:08 767 0

其实并不难用PHP自带的simplexml系列函数就可以实现

<?php
$xml=simplexml_load_file('http://www.site.com/rss.xml');
foreach($xml->channel->item as $item){
	echo '<a href="'.$item->link.'">';
	echo $item->title;
	echo '</a>';
	echo '<br/>';
	$des=str_replace(' ','',str_replace('	','',strip_tags($item->description)));
	$des=str_replace("\n",'',$des);
	$des=str_replace("\r",'',$des);
	echo $des;
	echo '<br/>';
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
<META HTTP-EQUIV="expires" CONTENT="0">
<meta http-equiv= "refresh" content= "300000" /> 
    <title>导航-热点标题</title>
    <base target="_blank" />
    <script type="text/javascript" src="http://libs.baidu.com/jquery/1.4.4/jquery.min.js"></script>
    <style>
*{ margin:0; padding:0;}
.scrollDiv {
height: 193px;
overflow: hidden;
margin: 0;
padding: 0;
}

.scrollDiv ul {
margin: 0;
padding: 0;
list-style:none;
}

.scrollDiv ul li {
font-size: 12px;
line-height: 23px;
height: 23px;
margin: 0;
padding: 0 0 0 8px;
width:190px;
overflow: hidden;
     text-overflow: ellipsis;/*文字隐藏后添加省略号*/
     white-space: nowrap;/*强制不换行*/    
}

h2{ font-size:14px; color:#0a57a3; height:20x; line-height:20px; margin:0; padding:0 0 5px 8px;}
.scrollDiv ul li a{ color:#555; text-decoration:none;}
h2 a:hover, .scrollDiv ul li a:hover{ text-decoration:underline; color:#f60;}
h2 a{ font-size:12px; color:#f60; text-decoration:none; font-weight:normal; margin-left:60px;}
</style>
    </head>
    <body>
<h2>搜索热点<a href="http://top.baidu.com/">百度热点&gt;&gt;</a></h2>
<div id="s2">
      <ul>
    <?php  

$xml=simplexml_load_file('https://www.178d.com/feed.php');
foreach($xml->channel->item as $item){
echo "<li>"."<a href=". $item->link .">" . $item->title . "</a></li>";   
}
?>

  </ul>
    </div>
<script type="text/javascript">

(function($){

$.fn.extend({

Scroll:function(opt,callback){

if(!opt) var opt={};

var _this=this.eq(0).find("ul:first");

var lineH=_this.find("li:first").height(), 

line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10),

speed=opt.speed?parseInt(opt.speed,10):500, 

timer=opt.timer?parseInt(opt.timer,10):3000; 

if(line==0) line=1;

var upHeight=0-line*lineH;

scrollUp=function(){

_this.animate({

marginTop:upHeight

},speed,function(){

for(i=1;i<=line;i++){

_this.find("li:first").appendTo(_this);

}

_this.css({marginTop:0});

});

}



_this.hover(function(){

clearInterval(timerID);

},function(){

timerID=setInterval("scrollUp()",timer);

}).mouseout();

}       

});

})(jQuery);

$(document).ready(function(){

$("#s2").Scroll({line:5,speed:600,timer:8000});

});

</script>
</body>
</html>