免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1417 | 回复: 0
打印 上一主题 下一主题

PHP 解析 RSS [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-13 11:08 |只看该作者 |倒序浏览
代码来自 IBM DW 英文版 (http://www.ibm.com/developerworks/library/x-phprss/)
php代码
  1. <?php

  2. require "XML/RSS.php";
  3. $rss = new XML_RSS("http://php.net/news.rss");
  4. $rss->parse();

  5. foreach($rss->getItems() as $item) {
  6.   print_r($item);
  7. }
  8. ?>
复制代码
RSS.php
  1. <?php

  2. $database =  "nameofthedatabase";
  3. $dbconnect = mysql_pconnect(localhost, dbuser, dbpassword);
  4. mysql_select_db($database, $dbconnect);
  5. $query = "select link, headline, description from `headlines` limit 15";
  6. $result = mysql_query($query, $dbconnect);

  7. while ($line = mysql_fetch_assoc($result))
  8.         {
  9.             $return[] = $line;
  10.         }

  11. $now = date("D, d M Y H:i:s T");

  12. $output = "<?xml version=\"1.0\"?>
  13.             <rss version=\"2.0\">
  14.                 <channel>
  15.                     <title>Our Demo RSS</title>
  16.                     <link>http://www.tracypeterson.com/RSS/RSS.php</link>
  17.                     <description>A Test RSS</description>
  18.                     <language>en-us</language>
  19.                     <pubDate>$now</pubDate>
  20.                     <lastBuildDate>$now</lastBuildDate>
  21.                     <docs>http://someurl.com</docs>
  22.                     <managingEditor>you@youremail.com</managingEditor>
  23.                     <webMaster>you@youremail.com</webMaster>
  24.             ";
  25.             
  26. foreach ($return as $line)
  27. {
  28.     $output .= "<item><title>".htmlentities($line['headline'])."</title>
  29.                     <link>".htmlentities($line['link'])."</link>
  30.                      
  31. <description>".htmlentities(strip_tags($line['description']))."</description>
  32.                 </item>";
  33. }
  34. $output .= "</channel></rss>";
  35. header("Content-Type: application/rss+xml");
  36. echo $output;
  37. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP