PHP 实现RSS 完整实例这是我曾经写的一篇学习记录。
该实例包括3个页面:
rss.php
mysql.class.php
rss.class.php
第一个页面:rss.php
< ? php
/*
* Created on 2010-11-5
*
* ETweb
*
*/
include_once ( 'libs/rss.class.php' ) ;
include_once ( 'libs/mysql.class.php' ) ;
#[数据库配置信息]
$ db_config = array (
'dbtype' = > 'mysql' ,
'dbhost' = > 'localhost' ,
'dbuser' = > 'root' ,
'dbpass' = > '' ,
'dbname' = > 'etweb' ,
'def' = > 'hzm_' , //表前缀
'charset' = > 'GBK' ,
'timezone' = > 'PRC' ,
'coding' = > 'deb1ba4390d2cfb1e973fe61ceef94a3' , //生成cookie加密
'version' = > '1.1' , //版本号
) ;
$ db = new mysql( $ db_config [ 'dbhost' ] , $ db_config [ 'dbuser' ] , $ db_config [ 'dbpass' ] , $ db_config [ 'dbname' ] , ALL_PS, $ db_config [ 'charset' ] ) ;
$ Rss_newnum = 20;
$ Rss_listnum = 20;
$ Rss_updatetime = 10;
$ title = "实现RSS订阅" ;
$ link = "" ;
$ db_bbsname = "天空从何开始" ;
$ db_bbsurl = "rss.php" ;
$ channel = array (
'title' = > $ title ,
'link' = > $ link ,
'description' = > "Latest $Rss_newnum replies of $title" ,
'copyright' = > "Copyright(C) $db_bbsname" ,
'generator' = > "ETweb by 天空从何开始" ,
'lastBuildDate' = > date ( 'r' ) ,
) ;
$ image = array (
'url' = > "templates/default/images/rss.png" ,
'title' = > 'ET website' ,
'link' = > $ db_bbsurl ,
'description' = > $ db_bbsname ,
) ;
$ oRSS = new Rss( array ( 'xml' = > "1.0" , 'rss' = > "2.0" , 'encoding' = > "gb2312" ) ) ;
$ oRSS - > channel( $ channel ) ;
$ oRSS - > image( $ image ) ;
$ 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 " ;
//联合查询 item中呈现的数据,这里是查询的一个新闻基表(hzm_news_base)及其分类(hzm_news_class)
$ result = $ db - > query( $ sql ) ;
while ( $ row = $ db - > fetch_array( $ result ) ) {
$ item = array (
"title" = > $ row [ title] ,
"description" = > $ row [ description] ,
"link" = > "article.php?id=$row[id]" ,
"author" = > $ row [ author] ,
"category" = > $ row [ name] ,
"pubdate" = > date ( "Y-m-d" , $ row [ date_time] )
) ;
$ oRSS - > item( $ item ) ;
}
$ all = $ oRSS - > rssHeader;
$ all . = $ oRSS - > rssChannel;
$ all . = $ oRSS - > rssImage;
$ all . = $ oRSS - > rssItem;
$ all . = "" ;
header ( "Content-type: application/xml" ) ;
echo $ all ; exit ;
? >
第2个页面:rss.class.php
/ / - - - - - - - - - - - - - - - - - - - - - - - 以下为rss. class . php 取自phpwind- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - / /
< ? php
/**
* Rss
*
* @package Rss
*/
class Rss {
public $ rssHeader ;
public $ rssChannel ;
public $ rssImage ;
public $ rssItem ;
function Rss( $ Rss = array ( 'xml' = > "1.0" , 'rss' = > "2.0" , 'encoding' = > "gb2312" ) ) {
$ this - > rssHeader = "\n" ;
$this- > rssHeader . = "\n" ;
}
function channel( $ channel) {
$this- > rssChannel = "\n" ;
foreach ( $ channel as $key = > $value ) {
$this- > rssChannel . = " <$key> . $value . "]]>$key>\n" ;
}
}
function image( $ image) {
$this- > rssImage = " \n" ;
foreach ( $ image as $key = > $value ) {
$this- > rssImage . = " <$key> . $value . "]]>$key>\n" ;
}
$this- > rssImage . = " \n" ;
}
function item( $ item) {
$this- > rssItem . = "- \n"
;
foreach ( $ item as $key = > $value ) {
$this- > rssItem . = " <$key> . $value . "]]>$key>\n" ;
}
$this- > rssItem . = "\n" ;
}
/ * *
* 写文件
*
* @ param string $fileName 文件绝对路径
* @ param string $data 数据
* @ param string $method 读写模式
* @ param bool $ifLock 是否锁文件
* @ param bool $ifCheckPath 是否检查文件名中的“. . ”
* @ param bool $ifChmod 是否将文件属性改为可读写
* /
function writeover( $ fileName, $data , $method = 'rb+' , $ifLock = true, $ifCheckPath = true, $ifChmod = true) {
/ / $ fileName = Pcv( $ fileName, $ifCheckPath) ;
touch( $ fileName) ;
$handle = fopen( $ fileName, $method ) ;
$ifLock & & flock( $ handle, LOCK_EX) ;
fwrite( $ handle, $data ) ;
$method = = 'rb+' & & ftruncate( $ handle, strlen( $ data ) ) ;
fclose( $ handle) ;
$ifChmod & & @ chmod( $ fileName, 0777) ;
}
function generate( $ rss_path) {
$all = $this- > rssHeader;
$all . = $this- > rssChannel;
$all . = $this- > rssImage;
$all . = $this- > rssItem;
$all . = "" ;
writeover( $ rss_path, $all) ;
}
}
? >
第3个页面mysql.class.php:
/ / - - - - - - - - - - - - - - - - - - - - - - - - - 以下为mysql. class . php 出自phpyun- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - / /
< ? php
/*
* Created on 2010
* Link for job@phpyun.com
* This PHPYun.Rencai System Powered by PHPYun.com
*/
class mysql {
private $ db_host ; //数据库主机
private $ db_user ; //数据库用户名
private $ db_pwd ; //数据库用户名密码
private $ db_database ; //数据库名
private $ conn ; //数据库连接标识;
private $ result ; //执行query命令的结果资源标识
private $ sql ; //sql执行语句
private $ row ; //返回的条目数
private $ coding ; //数据库编码,GBK,UTF8,gb2312
private $ bulletin = true ; //是否开启错误记录
private $ show_error = true ; //测试阶段,显示所有错误,具有安全隐患,默认关闭
private $ is_error = false ; //发现错误是否立即终止,默认true,建议不启用,因为当有问题时用户什么也看不到是很苦恼的
/*构造函数*/
public function __construct( $ db_host , $ db_user , $ db_pwd , $ db_database , $ conn , $ coding ) {
$ this - > db_host = $ db_host ;
$ this - > db_user = $ db_user ;
$ this - > db_pwd = $ db_pwd ;
$ this - > db_database = $ db_database ;
$ this - > conn = $ conn ;
$ this - > coding = $ coding ;
$ this - > connect( ) ;
}
/*数据库连接*/
public function connect( ) {
if ( $ this - > conn = = "pconn" ) {
//永久链接
$ this - > conn = mysql_pconnect ( $ this - > db_host, $ this - > db_user, $ this - > db_pwd) ;
} else {
//即使链接
$ this - > conn = mysql_connect ( $ this - > db_host, $ this - > db_user, $ this - > db_pwd) ;
}
if ( ! mysql_select_db ( $ this - > db_database, $ this - > conn) ) {
if ( $ this - > show_error) {
$ this - > show_error( "数据库不可用:" , $ this - > db_database) ;
}
}
mysql_query ( "SET NAMES $this->coding" ) ;
}
/*数据库执行语句,可执行查询添加修改删除等任何sql语句*/
public function query( $ sql ) {
if ( $ sql = = "" ) {
$ this - > show_error( "SQL语句错误:" , "SQL查询语句为空" ) ;
}
$ this - > sql = $ sql ;
$ result = mysql_query ( $ this - > sql, $ this - > conn) ;
if ( ! $ result ) {
//调试中使用,sql语句出错时会自动打印出来
if ( $ this - > show_error) {
$ this - > show_error( "错误SQL语句:" , $ this - > sql) ;
}
} else {
$ this - > result = $ result ;
}
return $ this - > result;
}
/*创建添加新的数据库*/
public function create_database( $ database_name ) {
$ database = $ database_name ;
$ sqlDatabase = 'create database ' . $ database ;
$ this - > query( $ sqlDatabase ) ;
}
/*查询服务器所有数据库*/
//将系统数据库与用户数据库分开,更直观的显示?
public function show_databases( ) {
$ this - > query( "show databases" ) ;
echo "现有数据库:" . $ amount = $ this - > db_num_rows( $ rs ) ;
echo " " ;
$ i = 1;
while ( $ row = $ this - > fetch_array( $ rs ) ) {
echo "$i $row[Database]" ;
echo " " ;
$ i + + ;
}
}
//以数组形式返回主机中所有数据库名
public function databases( ) {
$ rsPtr = mysql_list_dbs ( $ this - > conn) ;
$ i = 0;
$ cnt = mysql_num_rows ( $ rsPtr ) ;
while ( $ i < $ cnt ) {
$ rs [ ] = mysql_db_name ( $ rsPtr , $ i ) ;
$ i + + ;
}
return $ rs ;
}
/*查询数据库下所有的表*/
public function show_tables( $ database_name ) {
$ this - > query( "show tables" ) ;
echo "现有数据库:" . $ amount = $ this - > db_num_rows( $ rs ) ;
echo " " ;
$ i = 1;
while ( $ row = $ this - > fetch_array( $ rs ) ) {
$ columnName = "Tables_in_" . $ database_name ;
echo "$i $row[$columnName]" ;
echo " " ;
$ i + + ;
}
}
/*
mysql_fetch_row() array $row[0],$row[1],$row[2]
mysql_fetch_array() array $row[0] 或 $row[id]
mysql_fetch_assoc() array 用$row->content 字段大小写敏感
mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感
*/
/*取得结果数据*/
public function mysql_result_li( ) {
return mysql_result ( $ str ) ;
}
/*取得记录集,获取数组-索引和关联,使用$row['content'] */
public function fetch_array( ) {
return mysql_fetch_array ( $ this - > result) ;
}
//获取关联数组,使用$row['字段名']
public function fetch_assoc( ) {
return mysql_fetch_assoc ( $ this - > result) ;
}
//获取数字索引数组,使用$row[0],$row[1],$row[2]
public function fetch_row( ) {
return mysql_fetch_row ( $ this - > result) ;
}
//获取对象数组,使用$row->content
public function fetch_Object( ) {
return mysql_fetch_object ( $ this - > result) ;
}
/*取得上一步 INSERT 操作产生的 ID*/
public function insert_id( ) {
return mysql_insert_id ( ) ;
}
//指向确定的一条数据记录
public function db_data_seek( $ id ) {
if ( $ id > 0) {
$ id = $ id - 1;
}
if ( ! @ mysql_data_seek ( $ this - > result, $ id ) ) {
$ this - > show_error( "SQL语句有误:" , "指定的数据为空" ) ;
}
return $ this - > result;
}
// 根据select查询结果计算结果集条数
public function db_num_rows( ) {
if ( $ this - > result = = null ) {
if ( $ this - > show_error) {
$ this - > show_error( "SQL语句错误" , "暂时为空,没有任何内容!" ) ;
}
} else {
return mysql_num_rows ( $ this - > result) ;
}
}
// 根据insert,update,delete执行结果取得影响行数
public function db_affected_rows( ) {
return mysql_affected_rows ( ) ;
}
//输出显示sql语句
public function show_error( $ message = "" , $ sql = "" ) {
if ( ! $ sql ) {
echo "" . $ message . "" ;
echo " " ;
} else {
echo "" ;
echo "错误信息提示: " ;
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相关联
阅读(1695) | 评论(0) | 转发(1) |