PHP解析RSS的方法

yipeiwu_com6年前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);
}
?>

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;
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

浅析php中常量,变量的作用域和生存周期

在PHP脚本中变量主要有:内置超级全局变量,一般的变量,常量,全局变量,静态变量等等,我们在使用它们的时候除了要正确地知道它们的语法以外,更重要的是,我们要知道它们在本质上的区别与联系—...

用PHP书写安全的脚本代码

在PHP 4.2中,他们取消了那种老的做法!正如我将在这篇文章中解释的那样,作出这样的变化的目的是出于安全性的考虑。我们将研究PHP在处理表单提交及其它数据时的新的做法,并说明为什么这样...

PHP yield关键字功能与用法分析

本文实例讲述了PHP yield关键字功能与用法。分享给大家供大家参考,具体如下: yield 关键字是php5.5版本推出的一个特性。生成器函数的核心是yield关键字。它最简单的调用...

学习PHP session的传递方式

学习PHP session的传递方式

本文实例为大家分享了PHP session的三种传递方式,供大家参考,具体内容如下 既然学习到了就做下笔记,解决数据的共享,在也不要担心,什么时候还要你自己手动去设置打开cookie了!...

Thinkphp3.2.3整合phpqrcode生成带logo的二维码

Thinkphp中没有二维码相关的库,因此我们可以通过整合phpqrcode来完成生成二维码的功能。 下载phpqrcode 下载地址:http://phpqrcode.sourcefo...