本文实例讲述了PHP解析RSS的方法。分享给大家供大家参考。具体如下:
1. php代码如下:
<?php<br />require "XML/RSS.php";<br />$rss = new XML_RSS("http://php.net/news.rss");<br />$rss->parse();<br />foreach($rss->getItems() as $item) {<br /> print_r($item);<br />}<br />?>
2. RSS.php代码如下:
<?php<br />$database = "nameofthedatabase";<br />$dbconnect = mysql_pconnect(localhost, dbuser, dbpassword);<br />mysql_select_db($database, $dbconnect);<br />$query = "select link, headline, description from `headlines` limit 15";<br />$result = mysql_query($query, $dbconnect);<br />while ($line = mysql_fetch_assoc($result))<br />{<br /> $return[] = $line;<br />}<br />$now = date("D, d M Y H:i:s T");<br />$output = "<br /> <span style="color:transparent">~来1源gaodai#ma#com搞*代#码1网</span><abbr>搞代gaodaima码</abbr> <br /> <br /> <title>Our Demo RSS</title><br /> <link>http://www.tracypeterson.com/RSS/RSS.php<br /> A Test RSS<br /> en-us<br /> $now<br /> $now<br /> http://someurl.com<br /> [email protected]<br /> [email protected]<br /> ";<br />foreach ($return as $line)<br />{<br /> $output .= "<title>".htmlentities($line['headline'])."</title><br /> <link>".htmlentities($line['link'])."<br />".htmlentities(strip_tags($line['description']))."<br /> ";<br />}<br />$output .= "";<br />header("Content-Type: application/rss+xml");<br />echo $output;<br />?>
希望本文所述对大家的php程序设计有所帮助。