网站建设
  简约型网页设计套餐998
  实惠型网站建设套餐2580
  综合型网站制作套餐4980
  网站改版与网站维护
  行业网站建设方案
  大型网站建设解决方案
  企业网站建设流程
  帝网科技网站设计与网站制作
建站FAQ
·网站空间问题解答
·企业邮箱问题解答
 
酷站欣赏
·房产酷站(379)
·综合门户(8 9)
·建筑装饰(603)
·手机通讯(354)
·生活购物(376)
·医疗保健(199)
·文化摄影(602)
·休闲体育(399)
>>更多酷站欣赏
网站优化
·Google(谷歌)优化   ·百度(BaiDu)优化
·雅虎(Yahoo)优化    ·Alexa排名优化   
·Google AdSense   ·DMOZ目录提交  
建站知识
·网站建设知识·网站名词解释·网站运营知识
·网络营销知识·搜索引擎知识·实用技术文摘
网站推广
百度网站推广 google网站推广
搜狐网站推广 网易网站推广
新浪网站推广   雅虎网站推广
  您当前位置: 当前位置:帝网科技 >> web开发 >> PHP专栏 >> 浏览文章
 
 
PHP生成RSS类 (PHP类)
作者:编辑整理 来源:帝网科技 日期:2008年10月17日 点击数:


以下为引用的内容:

/**
 * rss操作类
 *
 * FeedCreator class v1.7.2
 * originally (c) Kai Blankenhorn
 * www.bitfolge.de
 * kaib@bitfolge.de
 *
 */
// your local timezone, set to "" to disable or for GMT
define("TIME_ZONE","");
/**
 * Version string.
 **/
define("FEEDCREATOR_VERSION", "www.273.cn");
/**
 * A FeedItem is a part of a FeedCreator feed.
 *
 * @author Kai Blankenhorn
 * @since 1.3
 */
class FeedItem extends HtmlDescribable {
 /**
  * Mandatory attributes of an item.
  */
 var $title, $description, $link;
 
 /**
  * Optional attributes of an item.
  */
 var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator;
 
 /**
  * Publishing date of an item. May be in one of the following formats:
  *
  * RFC 822:
  * "Mon, 20 Jan 03 18:05:41 +0400"
  * "20 Jan 03 18:05:41 +0000"
  *
  * ISO 8601:
  * "2003-01-20T18:05:41+04:00"
  *
  * Unix:
  * 1043082341
  */
 var $date;
 
 /**
  * Any additional elements to include as an assiciated array. All $key => $value pairs
  * will be included unencoded in the feed item in the form
  *     <$key>$value
  * Again: No encoding will be used! This means you can invalidate or enhance the feed
  * if $value contains markup. This may be abused to embed tags not implemented by
  * the FeedCreator class used.
  */
 var $additionalElements = Array();
 // on hold
 // var $source;
}
 
/**
 * An FeedImage may be added to a FeedCreator feed.
 * @author Kai Blankenhorn
 * @since 1.3
 */
class FeedImage extends HtmlDescribable {
 /**
  * Mandatory attributes of an image.
  */
 var $title, $url, $link;
 
 /**
  * Optional attributes of an image.
  */
 var $width, $height, $description;
}
 
/**
 * An HtmlDescribable is an item within a feed that can have a description that may
 * include HTML markup.
 */
class HtmlDescribable {
 /**
  * Indicates whether the description field should be rendered in HTML.
  */
 var $descriptionHtmlSyndicated;
 
 /**
  * Indicates whether and to how many characters a description should be truncated.
  */
 var $descriptionTruncSize;
 
 /**
  * Returns a formatted description field, depending on descriptionHtmlSyndicated and
  * $descriptionTruncSize properties
  * @return    string    the formatted description 
  */
 function getDescription() {
  $descriptionField = new FeedHtmlField($this->description);
  $descriptionField->syndicateHtml = $this->descriptionHtmlSyndicated;
  $descriptionField->truncSize = $this->descriptionTruncSize;
  return $descriptionField->output();
 }
}
/**
 * An FeedHtmlField describes and generates
 * a feed, item or image html field (probably a description). Output is
 * generated based on $truncSize, $syndicateHtml properties.
 * @author Pascal Van Hecke
 * @version 1.6
 */
class FeedHtmlField {
 /**
  * Mandatory attributes of a FeedHtmlField.
  */
 var $rawFieldContent;
 
 /**
  * Optional attributes of a FeedHtmlField.
  *
  */
 var $truncSize, $syndicateHtml;
 
 /**
  * Creates a new instance of FeedHtmlField.
  * @param  $string: if given, sets the rawFieldContent property
  */
 function FeedHtmlField($parFieldContent) {
  if ($parFieldContent) {
   $this->rawFieldContent = $parFieldContent;
  }
 }
 
  /**
  * Creates the right output, depending on $truncSize, $syndicateHtml properties.
  * @return string    the formatted field
  */
 function output() {
  // when field available and syndicated in html we assume
  // - valid html in $rawFieldContent and we enclose in CDATA tags
  // - no truncation (truncating risks producing invalid html)
  if (!$this->rawFieldContent) {
   $result = "";
  } elseif ($this->syndicateHtml) {
   $result = "rawFieldContent."]]>";
  } else {
   if ($this->truncSize and is_int($this->truncSize)) {
    $result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
   } else {
    $result = htmlspecialchars($this->rawFieldContent);
   }
  }
  return $result;
 }
}

/**
 * UniversalFeedCreator lets you choose during runtime which
 * format to build.
 * For general usage of a feed class, see the FeedCreator class
 * below or the example above.
 *
 * @since 1.3
 * @author Kai Blankenhorn
 */
class UniversalFeedCreator extends FeedCreator {
 var $_feed;
 
 function _setFormat($format) {
  switch (strtoupper($format)) {
  
   case "2.0":
    // fall through
   case "RSS2.0":
    $this->_feed = new RSSCreator20();
    break;
  
   case "0.91":
    // fall through
   case "RSS0.91":
    $this->_feed = new RSSCreator091();
    break;
  
   default:
    $this->_feed = new RSSCreator091();
    break;
  }
       
  $vars = get_object_vars($this);
  foreach ($vars as $key => $value) {
   // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
   if (!in_array($key, array("_feed", "contentType", "encoding"))) {
    $this->_feed->{$key} = $this->{$key};
   }
  }
 }
 
 /**
  * Creates a syndication feed based on the items previously added.
  *
  * @see        FeedCreator::addItem()
  * @param    string    format    format the feed should comply to. Valid values are:
  *   "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
  * @return    string    the contents of the feed.
  */
 function createFeed($format = "RSS0.91") {
  $this->_setFormat($format);
  return $this->_feed->createFeed();
 }
 
 
  /**
  * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
  * header may be sent to redirect the use to the newly created file.
  * @since 1.4
  *
  * @param string format format the feed should comply to. Valid values are:
  *   "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", "ATOM0.3", "HTML", "JS"
  * @param string filename optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
  * @param boolean displayContents optional send the content of the file or not. If true, the file will be sent in the body of the response.
  */
 function saveFeed($format="RSS0.91", $filename="", $displayContents=true) {
  $this->_setFormat($format);
  $this->_feed->saveFeed($filename, $displayContents);
 }

   /**
    * Turns on caching and checks if there is a recent version of this feed in the cache.
    * If there is, an HTTP redirect header is sent.
    * To effectively use caching, you should create the FeedCreator object and call this method
    * before anything else, especially before you do the time consuming task to build the feed
    * (web fetching, for example).
    *
    * @param   string   format   format the feed should comply to. Valid values are:
    *       "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
    * @param filename   string   optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
    * @param timeout int      optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
    */
   function useCached($format="RSS0.91", $filename="", $timeout=3600) {
      $this->_setFormat($format);
      $this->_feed->useCached($filename, $timeout);
   }
}

/**
 * FeedCreator is the abstract base implementation for concrete
 * implementations that implement a specific format of syndication.
 *
 * @abstract
 * @author Kai Blankenhorn
 * @since 1.4
 */
class FeedCreator extends HtmlDescribable {
 /**
  * Mandatory attributes of a feed.
  */
 var $title, $description, $link;
 
 
 /**
  * Optional attributes of a feed.
  */
 var $syndicationURL, $image, $language, $copyright, $pubDate, $lastBuildDate, $editor, $editorEmail, $webmaster, $category, $docs, $ttl, $rating, $skipHours, $skipDays;
 /**
 * The url of the external xsl stylesheet used to format the naked rss feed.
 * Ignored in the output when empty.
 */
 var $xslStyleSheet = "";
 
 
 /**
  * @access private
  */
 var $items = Array();
 
 
 /**
  * This feed's MIME content type.
  * @since 1.4
  * @access private
  */
 var $contentType = "application/xml";
 
 
 /**
  * This feed's character encoding.
  * @since 1.6.1
  **/
 var $encoding = "utf-8";
 
 
 /**
  * Any additional elements to include as an assiciated array. All $key => $value pairs
  * will be included unencoded in the feed in the form
  *     <$key>$value
  * Again: No encoding will be used! This means you can invalidate or enhance the feed
  * if $value contains markup. This may be abused to embed tags not implemented by
  * the FeedCreator class used.
  */
 var $additionalElements = Array();
  
   
 /**
  * Adds an FeedItem to the feed.
  *
  * @param object FeedItem $item The FeedItem to add to the feed.
  * @access public
  */
 function addItem($item) {
  $this->items[] = $item;
 }
 /**
  * 清空当前数组值
  *
  * @param object FeedItem $item The FeedItem to add to the feed.
  * @access public
  */
  function clearItem2Null() {
  $this->items = array();
 }
 
 /**
  * Truncates a string to a certain length at the most sensible point.
  * First, if there's a '.' character near the end of the string, the string is truncated after this character.
  * If there is no '.', the string is truncated after the last ' ' character.
  * If the string is truncated, " ..." is appended.
  * If the string is already shorter than $length, it is returned unchanged.
  *
  * @static
  * @param string    string A string to be truncated.
  * @param int        length the maximum length the string should be truncated to
  * @return string    the truncated string
  */
 function iTrunc($string, $length) {
  if (strlen($string)<=$length) {
   return $string;
  }
 
  $pos = strrpos($string,".");
  if ($pos>=$length-4) {
   $string = substr($string,0,$length-4);
   $pos = strrpos($string,".");
  }
  if ($pos>=$length*0.4) {
   return substr($string,0,$pos+1)." ...";
  }
 
  $pos = strrpos($string," ");
  if ($pos>=$length-4) {
   $string = substr($string,0,$length-4);
   $pos = strrpos($string," ");
  }
  if ($pos>=$length*0.4) {
   return substr($string,0,$pos)." ...";
  }
 
  return substr($string,0,$length-4)." ...";
  
 }
 
 
 /**
  * Creates a comment indicating the generator of this feed.
  * The format of this comment seems to be recognized by
  * Syndic8.com.
  */
 function _createGeneratorComment() {
  return "\n";
 }
 
 
 /**
  * Creates a string containing all additional elements specified in
  * $additionalElements.
  * @param elements array an associative array containing key => value pairs
  * @param indentString string a string that will be inserted before every generated line
  * @return    string    the XML tags corresponding to $additionalElements
  */
 function _createAdditionalElements($elements, $indentString="") {
  $ae = "";
  if (is_array($elements)) {
   foreach($elements AS $key => $value) {
    $ae.= $indentString."<$key>$value\n";
   }
  }
  return $ae;
 }
 
 function _createStylesheetReferences() {
  $xml = "";
  if ($this->cssStyleSheet) $xml .= "cssStyleSheet."\" _fcksavedurl="\"".$this->cssStyleSheet."\"" type=\"text/css\"?>\n";
  if ($this->xslStyleSheet) $xml .= "xslStyleSheet."\" type=\"text/xsl\"?>\n";
  return $xml;
 }
 
 
 /**
  * Builds the feed's text.
  * @abstract
  * @return    string    the feed's complete text
  */
 function createFeed() {
 }
 
 /**
  * Generate a filename for the feed cache file. The result will be $_SERVER["PHP_SELF"] with the extension changed to .xml.
  * For example:
  *
  * echo $_SERVER["PHP_SELF"]."\n";
  * echo FeedCreator::_generateFilename();
  *
  * would produce:
  *
  * /rss/latestnews.php
  * latestnews.xml
  *
  * @return string the feed cache filename
  * @since 1.4
  * @access private
  */
 function _generateFilename() {
  $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";
 }

  相关文章
 
·如何在PHP开启gzip页面压缩实例正文分
·如何正确运用PHP json_encode函数进行
·PHP下载断点续传的代码正文分析错误
·PHP取得客户端IP地址代码正文分析错误
·PHP判断用户IP来路的一个方法正文分析
·PHP+mysql分页代码正文分析错误
·php去除HTML标记正文分析错误
·php轻松快速缓存全站正文分析错误
·php多语言网站解决方案正文分析错误
·php图片验证码函数正文分析错误
·PHP初学者常见问题集(21问答)正文分
·分享PHP技术开发技巧正文分析错误
·cURL库功能简介:抓取网页 POST数据及
·PHP常用函数:过滤HTML字符串正文分析
·php fscanf()函数使用方法详解正文分析
·和php有关的几种常见安全详解正文分析
·php连接mysql出现乱码解决办法正文分析
·PHP非常简单的使用模板制作静态页面正
·非常简单PHP缩略图生成程序源代码正文
·无法载入 mcrypt 扩展,请检查 PHP 配置
 
 

公司环境 | 合作伙伴 | 人才招聘 | 付款方式 | 关于我们

地址:广州市天河区中山大道中120号D805 电话:020-82529556 传真:020-82529556
广州帝网网络科技有限公司 版权所有 粤ICP备08119341号