随笔 - 87  文章 - 279  trackbacks - 0
<2007年2月>
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910

潜心看书研究!

常用链接

留言簿(19)

随笔分类(81)

文章分类(89)

相册

ACM OJ

My friends

搜索

  •  

积分与排名

  • 积分 - 211878
  • 排名 - 116

最新评论

阅读排行榜

评论排行榜

没办法,近来靠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 阅读(344) 评论(0)  编辑 收藏 引用 所属分类: PHP之路

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理