PHP解析RSS的方法
程序员文章站
2022-11-15 16:55:09
本文实例讲述了php解析rss的方法。分享给大家供大家参考。具体如下:
1. php代码如下:
复制代码 代码如下:&...
本文实例讲述了php解析rss的方法。分享给大家供大家参考。具体如下:
1. php代码如下:
复制代码 代码如下:
<?php
require "xml/rss.php";
$rss = new xml_rss("http://php.net/news.rss");
$rss->parse();
foreach($rss->getitems() as $item) {
print_r($item);
}
?>
require "xml/rss.php";
$rss = new xml_rss("http://php.net/news.rss");
$rss->parse();
foreach($rss->getitems() as $item) {
print_r($item);
}
?>
2. rss.php代码如下:
复制代码 代码如下:
<?php
$database = "nameofthedatabase";
$dbconnect = mysql_pconnect(localhost, dbuser, dbpassword);
mysql_select_db($database, $dbconnect);
$query = "select link, headline, description from `headlines` limit 15";
$result = mysql_query($query, $dbconnect);
while ($line = mysql_fetch_assoc($result))
{
$return[] = $line;
}
$now = date("d, d m y h:i:s t");
$output = "<?xml version=\"1.0\"?>
<rss version=\"2.0\">
<channel>
<title>our demo rss</title>
<link>http://www.tracypeterson.com/rss/rss.php</link>
<description>a test rss</description>
<language>en-us</language>
<pubdate>$now</pubdate>
<lastbuilddate>$now</lastbuilddate>
<docs>http://someurl.com</docs>
<managingeditor>you@youremail.com</managingeditor>
<webmaster>you@youremail.com</webmaster>
";
foreach ($return as $line)
{
$output .= "<item><title>".htmlentities($line['headline'])."</title>
<link>".htmlentities($line['link'])."</link>
<description>".htmlentities(strip_tags($line['description']))."</description>
</item>";
}
$output .= "</channel></rss>";
header("content-type: application/rss+xml");
echo $output;
?>
$database = "nameofthedatabase";
$dbconnect = mysql_pconnect(localhost, dbuser, dbpassword);
mysql_select_db($database, $dbconnect);
$query = "select link, headline, description from `headlines` limit 15";
$result = mysql_query($query, $dbconnect);
while ($line = mysql_fetch_assoc($result))
{
$return[] = $line;
}
$now = date("d, d m y h:i:s t");
$output = "<?xml version=\"1.0\"?>
<rss version=\"2.0\">
<channel>
<title>our demo rss</title>
<link>http://www.tracypeterson.com/rss/rss.php</link>
<description>a test rss</description>
<language>en-us</language>
<pubdate>$now</pubdate>
<lastbuilddate>$now</lastbuilddate>
<docs>http://someurl.com</docs>
<managingeditor>you@youremail.com</managingeditor>
<webmaster>you@youremail.com</webmaster>
";
foreach ($return as $line)
{
$output .= "<item><title>".htmlentities($line['headline'])."</title>
<link>".htmlentities($line['link'])."</link>
<description>".htmlentities(strip_tags($line['description']))."</description>
</item>";
}
$output .= "</channel></rss>";
header("content-type: application/rss+xml");
echo $output;
?>
希望本文所述对大家的php程序设计有所帮助。
下一篇: PHP判断浏览器、判断语言代码分享