RSS PHP generated class, fast read build requires XML file

Source: Internet
Author: User
The RSS 2.0 version of PHP generated class is used in the project.

You can generate an XML file and output it through a function simply by inputting it into the data structure.

/** * RSS Generation module * @author jerryli (@lijian @dzs.mobi) * @see Protocol Description RSS specification follows 2.0 * http://jyjsjd.iteye.com/blog/1543483 * HT tp://rayleeya.iteye.com/blog/372802 */final class crssbuilder{/** * Local Character Set * @var String */const LOCAL_    CHARSET = ' GBK ';    /** * Language * @var String */Const LANGUAGE = ' ZH-CN ';    /** * RSS Owner * @var String */Const GENERATOR = ' www.csdn.net ';    /** * Plate Channel * @var array */private $_achannel = null; /** * constructor * @param string $aChannel Channel properties *
  • Array (' title ' + ' channel header ', ' description ' + ' Channel Summary ', ' link ' = ' Channel URL ')
  • */Public Function __construct ($aChannel) {header (' content-type:text/xml; Charset=utf-8 ');//must output standard XML header $this->_achannel = Array (' channel ' = = $aChannel, ' Items ' =>array ()); }/** * destructor */Public Function __destruct () {}/** * Add sub-item within channel * @param int $iChannelId Channel ID ( namely: AddChannel ()) *
  • Array (' title ' = ' + ' headline ', ' link ' = ' article ' url ', ' description ' + ' article abstract ', ' category ' = ' column name ', ' utc_timestemp ' = > ' Release time ')
  • * Number of @return int item */Public function Additems ($aParam) {$this->_achannel[' items '] [] = $aParam; Return count ($this->_achannel[' items '); /** * XML Structure data string that serializes content to RSS * @return String | NULL */Public Function getserialize () {static $sTemplateChannel = null; static $sTemplateItem = null; if (Empty ($sTemplateChannel) | | empty ($sTemplateItem)) {$aTmp = array (); $ATMP [] = ' '; $ATMP [] = ' . Self::language. ' '; $ATMP [] = ' . Self::generator. ' '; $ATMP [] = ' . (60 * 24). ' '; The update is 24 hours $aTmp [] = ' . Gmdate ("D, D M Y 23:59:59", Time ()). ' GMT '; Current RSS last Published time $aTmp [] = ' <! [Cdata[{@channel_title}]]>'; $ATMP [] = " {@channel_link} ; $ATMP [] = " {@channel_description} ; $ATMP [] = ' {@item} '; $ATMP [] = ' '; $sTemplateChannel = implode ("\ n", $ATMP); Unset ($ATMP); $ATMP = Array (); $ATMP [] = ' '; $ATMP [] = ' <![ cdata[{@item_title}]]> '; $ATMP [] = ' {@item_link} '; $ATMP [] = ' '; $ATMP [] = ' '; $ATMP [] = ' '; $ATMP [] = ' {@item_pubdate} '; Article release time $aTmp [] = ' '; $sTemplateItem = implode ("\ n", $ATMP); Unset ($ATMP); } if (!empty ($this->_achannel)) {$aTmpItem = array (); foreach ($this->_achannel[' items '] as $aItem) {//Inner layer traversal Item $aParam = Array (' {@item_ti tle} ' = self::convert_encoding ($aItem [' title '], Self::local_charset), ' {@item_link} ' and ' = $aItem [' Li NK '], ' {@item_description} ' = self::convert_encoding ($aItem [' description '], self::local_charset), ' {@item_category} ' = self::convert_encoding ($aItem [' Category '], Self::local_charset), ' {@item_pubdate} ' = Gmdate ("D, D M Y 23:59:59", $aItem [' utc_timestemp ']). ' GMT ',); $aTmpItem [] = Strtr ($sTemplateItem, $aParam); Unset ($aParam); $aParam =null; } $aParam = Array (' {@channel_title} ' = self::convert_encoding ($this->_achannel[' ChanneL ' [' title '], Self::local_charset), ' {@channel_link} ' = ' $this->_achannel[' channel ' [' Link '], ' {@channel_description} ' = self::convert_encoding ($this->_achannel[' channel ' [' Description '], Self::local_ CHARSET), ' {@item} ' = implode ("\ n", $aTmpItem),); $aOutBuf = Array (); $aOutBuf [] = ' '; $aOutBuf [] = ' '; $aOutBuf [] = Strtr ($sTemplateChannel, $aParam);; Unset ($aParam); $aParam =null; $aOutBuf [] = ' '; return implode ("\ n", $aOutBuf); }else{return null; }}/** * character encoding conversion of variable contents * @param string $sInCharset character set before conversion * @param string $sOutCharset converted character Set * @param string | Array $mixd the variable (array or string) to be converted * @return String | Array completes the converted result */static public Function convert_encoding (& $mixd, $sInCharset, $sOutCharset = ' utf-8 ') { if ($sInCharset = = = $sOutCharset)//character set is the same, return $mixd is not converted; if (Is_array ($MIXD)) {$tmp = array (); foreach ($mixd as $key = + $val) {$tmp [$key] = self::convert_encoding ($sInCharset, $sOutCharset, $val); } return $tmp; } else {//the same character set does not convert return mb_convert_encoding ($mixd, $sOutCharset, $sInCharset); } }}

    Use the time to change several constants into the parameters you need, and then you can output the standard RSS code as described below.

    This program uses GBK local environment character set, if your local environment is UTF-8, please modify this code local_charset = ' GBK ';

    /* Test Case */$aChannel = array (' title ' = ' Weekly featured ', ' description ' = ' Latest used Car news ', ' link ' = ' http://www.chemao.com.cn/ Baike-gonglue.html '); $o = new Crssbuilder ($aChannel); $aItem = Array (    ' title ' = ' Meet baby, get on the cat net to buy the right polo ',    ' Link ' = ' http://www.chemao.com.cn/baike/cwpolo.html ',    ' description ' = ' I love my husband in college, both of them are northerners. Because the family is suitable for their own employment opportunities are few, after graduating two people came to Hangzhou development. Two years of hard work, last July we finally ended 5 years of love long-distance running, in the West Lake with friends and relatives to witness our love. '    category ' = ' Weekly selection ',    ' utc_timestemp ' =>time ()), $o->additems ($aItem); $aItem = Array (    ' Title ' = ' To meet space needs, buy a second-hand MPV can be OK! ',    ' link ' = ' http://www.chemao.com.cn/baike/esmpv.html ',    ' description ' + ' with second child policy and universal entrepreneurship The gradual rise of the road on the more and more MPV, the powerful use of such models have been put down by many people. And the arrival of gold nine silver not only make new car concessions more and more big, the price of used cars is also a sharp dive, the price of MPV is "a derogatory and then derogatory", for those who want to start this kind of model, now is the perfect time! Below, we recommend several used car market common popular MPV. '    category ' = ' Weekly selection ',    ' utc_timestemp ' =>time ()); $o->additems ($aItem); Echo $o->getserialize ();

    The above describes the RSS PHP generation class, fast-read generation needs XML files, including the entrepreneurial aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.