PHP解析RSS的方法

yipeiwu_com5年前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程序占用多少内存(memory_get_usage)

下面是使用示例: 复制代码 代码如下: <?php echo memory_get_usage(), '<br />'; // 313864 $tmp = str_re...

PHP实现移除数组中为空或为某值元素的方法

本文实例讲述了PHP实现移除数组中为空或为某值元素的方法。分享给大家供大家参考,具体如下: 在实现移除数组中项目为空的元素或为某值的元素时用到了两个函数 array_filter、cre...

php base64 编码与解码实例代码

php base64 编码与解码详解 1.自定义规则方式编码和解码 实例 public function test_changinttoStr () { $intvalue...

浅析PHP类的反射来实现依赖注入过程

PHP具有完整的反射 API,提供了对类、接口、函数、方法和扩展进行逆向工程的能力。通过类的反射提供的能力我们能够知道类是如何被定义的,它有什么属性、什么方法、方法都有哪些参数,类文件的...

探究Laravel使用env函数读取环境变量为null的问题

探究Laravel使用env函数读取环境变量为null的问题

发现问题 在 Laravel 项目中,如果执行了 php artisan config:cache 命令把配置文件缓存起来后,在 Tinker 中(Tinker 是 Laravel 自带...