没办法,近来靠php吃饭-_-
<?
/*************
| +-------------------------------------------------
| Id:
| +-------------------------------------------------
| Copyright (c)
| Author:Howie
| Email: qywyh_scut@163.com
| +-------------------------------------------------
| Create Date: 2007-2-3
| Modify Date:
| Note:
| demo:
| $objRSS = new loadRSS('http://news.163.com/special/00011K6L/rss_gn.xml', 10, 'cache/test.xml');
| echo $objRSS->getRSS();
|
| +-------------------------------------------------
***************/
class loadRSS {
//RSS url链接
var $_url = '';
//RSS 缓存目录
var $_cacheDir = '';
//缓存时间
var $_cacheTime = '';
//是否从缓存读取
var $_loadFromCache = true;
//保存rss字符串
var $_str = '';
function loadRSS ($url, $cacheTime=0, $cacheDir=''){
$this->_url = $url;
if ($cacheDir == '' || $cacheTime == 0) {
$this->_loadFromCache = false;
} else {
$this->_cacheTime = $cacheTime;
$this->_cacheDir = $cacheDir;
}
}
function getRSS () {
if (!$this->_loadFromCache) {
$fp = fopen($this->_url, 'r');
while ($tmp = fgets($fp, 4096)) {
$this->_str .= $tmp;
}
fclose($fp);
//echo $this->_str;
} else {
$fileModifyTime = @filemtime($this->_cacheDir);
$nowTime = time();
//修改缓存文件
if (!$fileModifyTime || $nowTime-$fileModifyTime >= $this->_cacheTime) {
$fp = fopen($this->_url, 'r');
$fp2 = fopen($this->_cacheDir, 'w');
while ($tmp = fgets($fp, 4096)) {
fwrite($fp2, $tmp);
$this->_str .= $tmp;
}
fclose($fp);
fclose($fp2);
return $this->_str;
}
$fp = fopen($this->_cacheDir, 'r');
while ($tmp = fgets($fp, 4096)) {
$this->_str .= $tmp;
}
fclose($fp);
}
return $this->_str;
}
}
?>
posted on 2007-02-03 01:32
豪 阅读(353)
评论(0) 编辑 收藏 引用 所属分类:
PHP之路