Jump to navigation

You are currently browsing all posts tagged with 'class'

具有缓存功能的php rss类

  • Posted on December 30, 2009 at 11:08 am
<?PHP

/**
 * rss2 类
 *    具有缓存功能的rss2输出类
 * @author: 190890101@qq.com  花荣
 * @version: rss.class.php v0.0.1 2009/12/30
 * @usage:
 * $rss = new rss("GBK","花荣老师的博客","http://www.masalife.com","这里是描述", "2009-12-28 11:23:33", "en", $_SERVER['DOCUMENT_ROOT']."/cache/a.xml", 3600);
 * if ($rss->need_to_add_item())
 * {
 *    //read from mysql and add items;
 *    while(($row=mysql_fetch_array($r)) !== false)
 *    {
 *        $item['title'] = $row['fName'];
 *        $item['link'] = "http://www.masalife.com/post.php?pid=".$row['ID']."";
 *        $item['pubDate'] = $row['fPubDate'];
 *        $item['guid']  = $item['link'];
 *        $item['description'] = $item['content'];
 *        $this->add_item($item);
 *    }
 * }
 * echo $rss->show();
 **/
class rss
{

 protected $version="2.0";        //rss版本
 protected $encoding = "GBK";        //编码

 protected $channel_title = "";
 protected $channel_link = "";
 protected $channel_description = "";
 protected $channel_pubDate = "";
 protected $channel_language = "en";

 protected $items = array();        //item数组,来自外部的数据源
 protected $xml = "";            //最终生成的xml

 protected $cache_timeout = 3600;    //cache失效时间(秒)
 protected $cache_filename= "";        //cache文件
 protected $cache_valid = false;        //cache文件是否有效

 /**
 * 构造函数
 * @param $title
 * @param $link
 * @param $description
 * @param $pubDate
 * @param $language
 * @param $cache_filename    cache文件名
 * @param $cache_timeout    cache失效时间(秒)
 * @return none
 **/
 public function __construct($encoding, $title, $link, $description, $pubDate, $language, $cache_filename, $cache_timeout)
 {
 $this->encoding  = trim($encoding);
 $this->channel_title = trim($title);
 $this->channel_link = trim($link);
 $this->channel_description = trim($description);
 $this->channel_pubDate = trim($pubDate);
 $this->channel_language = trim($language);
 $this->cache_filename = trim($cache_filename);
 $this->cache_timeout = intval($cache_timeout);
 if ($this->cache_timeout==0) $this->cache_timeout = 3600;
 //检查cache文件是否有效
 $this->is_cache_valid();
 }

 /**
 * cache文件是否有效
 **/
 protected function is_cache_valid()
 {
 if (file_exists($this->cache_filename) && time() - filemtime($this->cache_filename) < $this->cache_timeout)
 {
 $this->cache_valid = true;
 }
 else
 {
 $this->cache_valid = false;
 }
 }

 /**
 * 是否需要添加item. cache文件已经失效的话,就需要从外部数据源添加item了
 * @return boolean
 **/

 public function need_to_add_item()
 {
 return !$this->cache_valid;
 }

 /**
 * 构造并返回xml
 * @param &$xml
 **/
 public function show(&$xml)
 {
 if ($this->cache_valid)
 {
 $this->read_from_file();
 }
 else
 {
 $this->make();
 $this->save_to_file();
 }
 $xml = $this->xml;
 }

 /**
 * 构造xml
 **/
 protected function make()
 {
 $s   =   "<?xml   version=\"1.0\"   encoding=\"".$this->encoding."\"   ?>\n<rss   version=\"".$this->version."\">   \n";  

 $s   .=   "<channel>\n";  
 $s   .=   "<title>{$this->channel_title}</title>\n";
 $s   .=   "<link>{$this->channel_link}</link>\n";  
 $s   .=   "<description>{$this->channel_description}</description>\n";  
 $s   .=   "<language>{$this->channel_language}</language>\n";  
 if   (!empty($this->pubDate))   {  
 $s   .=   "<pubDate>{$this->channel_pubDate}</pubDate>\n";  
 }  

 //   start   items  
 $count = count($this->items);
 for ($i=0; $i<$count; $i++)
 {
 $s   .=   "<item>\n";  
 $s   .=   "<title>{$this->items[$i]['title']}</title>\n";  
 $s   .=   "<link>{$this->items[$i]['link']}</link>\n";  
 $s   .=   "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\n";                        
 $s   .=   "<guid>{$this->items[$i]['guid']}</guid>\n";                        
 $s   .=   "<description><![CDATA[{$this->items[$i]['description']}]]></description>\n";  
 $s   .=   "</item>\n";  
 }  

 //   close   channel  
 $s   .=   "</channel>\n</rss>";  
 $this->xml =   $s;   

 }

 /**
 * 添加item
 * item需要有title, link, pubDate, guid, description这几个标签
 * @param $item    数组
 **/
 public function add_item($item=array())
 {
 $this->items[] = $item;
 }

 /**
 * 保存xml到cache文件
 **/
 protected function save_to_file()
 {
 file_put_contents($this->cache_filename, $this->xml, LOCK_EX);
 }

 /**
 * 从cache文件中读出xml
 **/
 protected function read_from_file()
 {
 $this->xml = file_get_contents($this->cache_filename);
 }

}

Top