Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2531057
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: 系统运维

2011-07-18 16:56:37

 
PHP 实现RSS 完整实例这是我曾经写的一篇学习记录。
该实例包括3个页面:
rss.php
mysql.class.php
rss.class.php
 
 
第一个页面:rss.php
 
  1. <?php
  2. /*
  3. * Created on 2010-11-5
  4. *
  5. * ETweb
  6. *
  7. */

  8. include_once('libs/rss.class.php');
  9. include_once('libs/mysql.class.php');

  10. #[数据库配置信息]

  11. $db_config = array(
  12. 'dbtype'=>'mysql',
  13. 'dbhost'=>'localhost',
  14. 'dbuser'=>'root',
  15. 'dbpass'=>'',
  16. 'dbname'=>'etweb',
  17. 'def'=>'hzm_',//表前缀

  18. 'charset'=>'GBK',
  19. 'timezone'=>'PRC',
  20. 'coding'=>'deb1ba4390d2cfb1e973fe61ceef94a3', //生成cookie加密

  21. 'version'=>'1.1',//版本号

  22. );
  23. $db = new mysql($db_config['dbhost'], $db_config['dbuser'], $db_config['dbpass'], $db_config['dbname'], ALL_PS, $db_config['charset']);


  24. $Rss_newnum=20;
  25. $Rss_listnum=20;
  26. $Rss_updatetime=10;

  27. $title ="实现RSS订阅";
  28. $link = "";
  29. $db_bbsname ="天空从何开始";
  30. $db_bbsurl = "rss.php";

  31. $channel = array(
  32. 'title' => $title,
  33. 'link' => $link,
  34. 'description' => "Latest $Rss_newnum replies of $title",
  35. 'copyright' => "Copyright(C) $db_bbsname",
  36. 'generator' => "ETweb by 天空从何开始",
  37. 'lastBuildDate' => date('r'),
  38. );

  39. $image = array(
  40. 'url' => "templates/default/images/rss.png",
  41. 'title' => 'ET website',
  42. 'link' => $db_bbsurl,
  43. 'description' => $db_bbsname,
  44. );

  45. $oRSS = new Rss(array('xml'=>"1.0",'rss'=>"2.0",'encoding'=>"gb2312"));
  46. $oRSS->channel($channel);

  47. $oRSS->image($image);

  48. $sql = "select b.id,b.classid,b.title,b.author,b.date_time,b.description,class.id,class.name from hzm_news_base b left join hzm_news_class class on b.classid=class.id order by b.id desc limit $Rss_listnum ";
  49. //联合查询 item中呈现的数据,这里是查询的一个新闻基表(hzm_news_base)及其分类(hzm_news_class)


  50. $result = $db->query($sql);
  51. while($row = $db->fetch_array($result)){
  52. $item = array(

  53. "title"=>$row[title],
  54. "description"=>$row[description],
  55. "link"=>"article.php?id=$row[id]",
  56. "author"=>$row[author],
  57. "category"=>$row[name],
  58. "pubdate"=>date("Y-m-d",$row[date_time])

  59. );
  60. $oRSS->item($item);
  61. }



  62. $all = $oRSS->rssHeader;
  63. $all .= $oRSS->rssChannel;
  64. $all .= $oRSS->rssImage;
  65. $all .= $oRSS->rssItem;
  66. $all .= "";

  67. header("Content-type: application/xml");
  68. echo $all;exit;
  69. ?>
 
 
第2个页面:rss.class.php
  1. //-----------------------以下为rss.class.php 取自phpwind--------------------------------//
  2. <?php


  3. /**
  4. * Rss
  5. *
  6. * @package Rss
  7. */
  8. class Rss {

  9. public $rssHeader;
  10. public $rssChannel;
  11. public $rssImage;
  12. public $rssItem;

  13. function Rss($Rss = array('xml'=>"1.0",'rss'=>"2.0",'encoding'=>"gb2312")) {

  14. $this->rssHeader = "\n";
  15. $this->rssHeader .= "\n";
  16. }

  17. function channel($channel) {

  18. $this->rssChannel = "\n";
  19. foreach ($channel as $key => $value) {
  20. $this->rssChannel .= " <$key> . $value . "]]>\n";
  21. }
  22. }

  23. function image($image) {

  24. $this->rssImage = " \n";
  25. foreach ($image as $key => $value) {
  26. $this->rssImage .= " <$key> . $value . "]]>\n";
  27. }
  28. $this->rssImage .= " \n";
  29. }

  30. function item($item) {

  31. $this->rssItem .= "\n";
  32. foreach ($item as $key => $value) {
  33. $this->rssItem .= " <$key> . $value . "]]>\n";
  34. }
  35. $this->rssItem .= "\n";
  36. }
  37. /**
  38. * 写文件
  39. *
  40. * @param string $fileName 文件绝对路径
  41. * @param string $data 数据
  42. * @param string $method 读写模式
  43. * @param bool $ifLock 是否锁文件
  44. * @param bool $ifCheckPath 是否检查文件名中的“..
  45. * @param bool $ifChmod 是否将文件属性改为可读写
  46. */
  47. function writeover($fileName, $data, $method = 'rb+', $ifLock = true, $ifCheckPath = true, $ifChmod = true) {
  48. //$fileName = Pcv($fileName, $ifCheckPath);
  49. touch($fileName);
  50. $handle = fopen($fileName, $method);
  51. $ifLock && flock($handle, LOCK_EX);
  52. fwrite($handle, $data);
  53. $method == 'rb+' && ftruncate($handle, strlen($data));
  54. fclose($handle);
  55. $ifChmod && @chmod($fileName, 0777);
  56. }

  57. function generate($rss_path) {

  58. $all = $this->rssHeader;
  59. $all .= $this->rssChannel;
  60. $all .= $this->rssImage;
  61. $all .= $this->rssItem;
  62. $all .= "";
  63. writeover($rss_path, $all);
  64. }
  65. }
  66. ?>
 
第3个页面mysql.class.php:
  1. //-------------------------以下为mysql.class.php 出自phpyun-------------------------------//

  2. <?php
  3. /*
  4. * Created on 2010
  5. * Link for job@phpyun.com
  6. * This PHPYun.Rencai System Powered by PHPYun.com
  7. */

  8. class mysql {
  9. private $db_host; //数据库主机

  10. private $db_user; //数据库用户名

  11. private $db_pwd; //数据库用户名密码

  12. private $db_database; //数据库名

  13. private $conn; //数据库连接标识;

  14. private $result; //执行query命令的结果资源标识

  15. private $sql; //sql执行语句

  16. private $row; //返回的条目数

  17. private $coding; //数据库编码,GBK,UTF8,gb2312

  18. private $bulletin = true; //是否开启错误记录

  19. private $show_error = true; //测试阶段,显示所有错误,具有安全隐患,默认关闭

  20. private $is_error = false; //发现错误是否立即终止,默认true,建议不启用,因为当有问题时用户什么也看不到是很苦恼的


  21. /*构造函数*/
  22. public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
  23. $this->db_host = $db_host;
  24. $this->db_user = $db_user;
  25. $this->db_pwd = $db_pwd;
  26. $this->db_database = $db_database;
  27. $this->conn = $conn;
  28. $this->coding = $coding;
  29. $this->connect();
  30. }

  31. /*数据库连接*/
  32. public function connect() {
  33. if ($this->conn == "pconn") {
  34. //永久链接

  35. $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
  36. } else {
  37. //即使链接

  38. $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
  39. }

  40. if (!mysql_select_db($this->db_database, $this->conn)) {
  41. if ($this->show_error) {
  42. $this->show_error("数据库不可用:", $this->db_database);
  43. }
  44. }
  45. mysql_query("SET NAMES $this->coding");
  46. }

  47. /*数据库执行语句,可执行查询添加修改删除等任何sql语句*/
  48. public function query($sql) {
  49. if ($sql == "") {
  50. $this->show_error("SQL语句错误:", "SQL查询语句为空");
  51. }
  52. $this->sql = $sql;

  53. $result = mysql_query($this->sql, $this->conn);

  54. if (!$result) {
  55. //调试中使用,sql语句出错时会自动打印出来

  56. if ($this->show_error) {
  57. $this->show_error("错误SQL语句:", $this->sql);
  58. }
  59. } else {
  60. $this->result = $result;
  61. }
  62. return $this->result;
  63. }

  64. /*创建添加新的数据库*/
  65. public function create_database($database_name) {
  66. $database = $database_name;
  67. $sqlDatabase = 'create database ' . $database;
  68. $this->query($sqlDatabase);
  69. }

  70. /*查询服务器所有数据库*/
  71. //将系统数据库与用户数据库分开,更直观的显示?

  72. public function show_databases() {
  73. $this->query("show databases");
  74. echo "现有数据库:" . $amount = $this->db_num_rows($rs);
  75. echo "
    "
    ;
  76. $i = 1;
  77. while ($row = $this->fetch_array($rs)) {
  78. echo "$i $row[Database]";
  79. echo "
    "
    ;
  80. $i++;
  81. }
  82. }

  83. //以数组形式返回主机中所有数据库名

  84. public function databases() {
  85. $rsPtr = mysql_list_dbs($this->conn);
  86. $i = 0;
  87. $cnt = mysql_num_rows($rsPtr);
  88. while ($i < $cnt) {
  89. $rs[] = mysql_db_name($rsPtr, $i);
  90. $i++;
  91. }
  92. return $rs;
  93. }

  94. /*查询数据库下所有的表*/
  95. public function show_tables($database_name) {
  96. $this->query("show tables");
  97. echo "现有数据库:" . $amount = $this->db_num_rows($rs);
  98. echo "
    "
    ;
  99. $i = 1;
  100. while ($row = $this->fetch_array($rs)) {
  101. $columnName = "Tables_in_" . $database_name;
  102. echo "$i $row[$columnName]";
  103. echo "
    "
    ;
  104. $i++;
  105. }
  106. }

  107. /*
  108. mysql_fetch_row() array $row[0],$row[1],$row[2]
  109. mysql_fetch_array() array $row[0] 或 $row[id]
  110. mysql_fetch_assoc() array 用$row->content 字段大小写敏感
  111. mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感
  112. */

  113. /*取得结果数据*/
  114. public function mysql_result_li() {
  115. return mysql_result($str);
  116. }

  117. /*取得记录集,获取数组-索引和关联,使用$row['content'] */
  118. public function fetch_array() {
  119. return mysql_fetch_array($this->result);
  120. }

  121. //获取关联数组,使用$row['字段名']

  122. public function fetch_assoc() {
  123. return mysql_fetch_assoc($this->result);
  124. }

  125. //获取数字索引数组,使用$row[0],$row[1],$row[2]

  126. public function fetch_row() {
  127. return mysql_fetch_row($this->result);
  128. }

  129. //获取对象数组,使用$row->content

  130. public function fetch_Object() {
  131. return mysql_fetch_object($this->result);
  132. }

  133. /*取得上一步 INSERT 操作产生的 ID*/
  134. public function insert_id() {
  135. return mysql_insert_id();
  136. }

  137. //指向确定的一条数据记录

  138. public function db_data_seek($id) {
  139. if ($id > 0) {
  140. $id = $id -1;
  141. }
  142. if (!@ mysql_data_seek($this->result, $id)) {
  143. $this->show_error("SQL语句有误:", "指定的数据为空");
  144. }
  145. return $this->result;
  146. }

  147. // 根据select查询结果计算结果集条数

  148. public function db_num_rows() {
  149. if ($this->result == null) {
  150. if ($this->show_error) {
  151. $this->show_error("SQL语句错误", "暂时为空,没有任何内容!");
  152. }
  153. } else {
  154. return mysql_num_rows($this->result);
  155. }
  156. }

  157. // 根据insert,update,delete执行结果取得影响行数

  158. public function db_affected_rows() {
  159. return mysql_affected_rows();
  160. }

  161. //输出显示sql语句

  162. public function show_error($message = "", $sql = "") {
  163. if (!$sql) {
  164. echo "" . $message . "";
  165. echo "
    "
    ;
  166. } else {
  167. echo "
    ";
  168. echo "错误信息提示:
    "
    ;
  169. echo "
    ";
  170. echo "
    ";
  171. echo "错误号:12142";
  172. echo "

";
  • echo "错误原因:" . mysql_error() . "

    "
    ;
  • echo "
    ";
  • echo "" . $message . "";
  • echo "
  • ";
  • echo "
    " . $sql . "
    "
    ;
  • $ip = $this->getip();
  • if ($this->bulletin) {
  • $time = date("Y-m-d H:i:s");
  • $message = $message . "\r\n$this->sql" . "\r\n客户IP:$ip" . "\r\n时间 :$time" . "\r\n\r\n";

  • $server_date = date("Y-m-d");
  • $filename = $server_date . ".txt";
  • $file_path = "error/" . $filename;
  • $error_content = $message;
  • //$error_content="错误的数据库,不可以链接";

  • $file = "error"; //设置文件保存目录


  • //建立文件夹

  • if (!file_exists($file)) {
  • if (!mkdir($file, 0777)) {
  • //默认的 mode 是 0777,意味着最大可能的访问权

  • die("upload files directory does not exist and creation failed");
  • }
  • }

  • //建立txt日期文件

  • if (!file_exists($file_path)) {

  • //echo "建立日期文件";

  • fopen($file_path, "w+");

  • //首先要确定文件存在并且可写

  • if (is_writable($file_path)) {
  • //使用添加模式打开$filename,文件指针将会在文件的开头

  • if (!$handle = fopen($file_path, 'a')) {
  • echo "不能打开文件 $filename";
  • exit;
  • }

  • //将$somecontent写入到我们打开的文件中。

  • if (!fwrite($handle, $error_content)) {
  • echo "不能写入到文件 $filename";
  • exit;
  • }

  • //echo "文件 $filename 写入成功";


  • echo "——错误记录被保存!";

  • //关闭文件

  • fclose($handle);
  • } else {
  • echo "文件 $filename 不可写";
  • }

  • } else {
  • //首先要确定文件存在并且可写

  • if (is_writable($file_path)) {
  • //使用添加模式打开$filename,文件指针将会在文件的开头

  • if (!$handle = fopen($file_path, 'a')) {
  • echo "不能打开文件 $filename";
  • exit;
  • }

  • //将$somecontent写入到我们打开的文件中。

  • if (!fwrite($handle, $error_content)) {
  • echo "不能写入到文件 $filename";
  • exit;
  • }

  • //echo "文件 $filename 写入成功";

  • echo "——错误记录被保存!";

  • //关闭文件

  • fclose($handle);
  • } else {
  • echo "文件 $filename 不可写";
  • }
  • }

  • }
  • echo "
    "
    ;
  • if ($this->is_error) {
  • exit;
  • }
  • }
  • echo "
  • ";
  • echo "";

  • echo "
    "
    ;
  • }

  • //释放结果集

  • public function free() {
  • @ mysql_free_result($this->result);
  • }

  • //数据库选择

  • public function select_db($db_database) {
  • return mysql_select_db($db_database);
  • }

  • //查询字段数量

  • public function num_fields($table_name) {
  • //return mysql_num_fields($this->result);

  • $this->query("select * from $table_name");
  • echo "
    "
    ;
  • echo "字段数:" . $total = mysql_num_fields($this->result);
  • echo "
    ";
  • for ($i = 0; $i < $total; $i++) {
  • print_r(mysql_fetch_field($this->result, $i));
  • }
  • echo "";
  • echo "
    "
    ;
  • }

  • //取得 MySQL 服务器信息

  • public function mysql_server($num = '') {
  • switch ($num) {
  • case 1 :
  • return mysql_get_server_info(); //MySQL 服务器信息

  • break;

  • case 2 :
  • return mysql_get_host_info(); //取得 MySQL 主机信息

  • break;

  • case 3 :
  • return mysql_get_client_info(); //取得 MySQL 客户端信息

  • break;

  • case 4 :
  • return mysql_get_proto_info(); //取得 MySQL 协议信息

  • break;

  • default :
  • return mysql_get_client_info(); //默认取得mysql版本信息

  • }
  • }

  • //析构函数,自动关闭数据库,垃圾回收机制

  • public function __destruct() {
  • if (!empty ($this->result)) {
  • $this->free();
  • }
  • mysql_close($this->conn);
  • } //function __destruct();



  • }
  • ?>
  • //----------------------------------以下为两个表的信息-------------------------------------//
    hzm_news_base字段
    `id`, `classid`, `title`, `author`, `date_time`, `hit`, `keyword`, `tag`, `description`, `newsphoto`, `describe`, `ifcheck`

    hzm_news_class字段
    `id`, `name`, `fid`, `sort`
    其中hzm_news_base.classid 与hzm_news_class.id相关联
     
    阅读(1617) | 评论(0) | 转发(1) |
    给主人留下些什么吧!~~