RSS20php generation class, quick read generation requires xml files

Source: Internet
Author: User
: This article mainly introduces the RSS20php generation class, which requires an xml file for quick read generation. For more information about PHP tutorials, see. The php generation class of RSS 2.0 used in the project.

You only need to input data according to the data structure to generate an xml file and output it through the function.

/*** Rss generation module * @ author JerryLi (@ lijian@dzs.mobi) * @ see Protocol Description RSS specifications follow 2.0 * http://jyjsjd.iteye.com/blog/1543483 * http://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 attribute *
  • Array ('title' => 'Channel title', 'description' => 'Channel summary', 'link' => 'Current channel URL address ')
  • */Public function _ construct ($ aChannel) {header ('content-Type: text/xml; charset = UTF-8 '); // The standard xml header $ this-> _ aChannel = array ('channel' => $ aChannel, 'items '=> array () must be output ());} /*** destructor */public function _ destruct () {}/ *** add a sub-project in the Channel * @ param int $ iChannelId channel ID (I .e: addChannel ())*
  • Array ('title' => 'title', 'link' => 'Article URL address', 'description' => 'Article summary ', 'category '=> 'topic name', 'utc _ timestemp' => 'release time ')
  • * @ Return int number of items */public function addItems ($ aParam) {$ this-> _ aChannel ['items '] [] = $ aParam; return count ($ this-> _ aChannel ['items ']);} /*** serialize the content to the xml structure data string of 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 time is 24 hours. $ aTmp [] =' '. Gmdate ("D, d m y 23:59:59", time ().' GMT '; // The last release time of the current RSS feed $ 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 [] =' {@item_description} '; $ ATmp [] =' {@item_category} '; $ ATmp [] =' {@item_link} '; $ ATmp [] =' {@ Item_pubdate} '; // Article Release Date $ aTmp [] =' '; $ STemplateItem = implode ("\ n", $ aTmp); unset ($ aTmp);} if (! Empty ($ this-> _ aChannel) {$ aTmpItem = array (); foreach ($ this-> _ aChannel ['items '] as $ aItem) {// traverse the inner layer of item $ aParam = array ('{@ item_title}' => self: convert_encoding ($ aItem ['title'], self: LOCAL_CHARSET ), '{@ item_link}' => $ aItem ['link'], '{@ 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 ;}} /*** encode and convert Variable content * @ param string $ Character set before conversion * @ param string $ Character set after sOutCharset conversion * @ param string | array $ mixd variable to be converted (array or string) * @ return string | result of array conversion */static public function convert_encoding (& $ mixd, $ sInCharset, $ sOutCharset = 'utf-8 ') {if ($ sInCharset ===$ sOutCharset) // return $ mixd; if (is_array ($ mixd) {$ tmp = array (); foreach ($ mixd as $ key => $ val) {$ tmp [$ key] = self: convert_encoding ($ sInCharset, $ sOutCharset, $ val );} return $ tmp;} else {// return mb_convert_encoding ($ mixd, $ sOutCharset, $ sInCharset );}}}

    Modify the constants to the parameters you need, and then output the standard rss code as follows.

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

    /* Test case */$ aChannel = array ('title' => 'weekly Select', 'description' => 'latest used car News ', 'link' => 'http: // www.chemao.com.cn/baike-gonglue.html'); $ o = new CRssBuilder ($ aChannel); $ aItem = array ('title' => 'Meet the Baby, get on the cat's net sweet favorite Polo ', 'link' => 'http: // www.chemao.com.cn/javaske/cwpolo.html', 'description' => 'my husband and I love each other in college. Because there are few job opportunities suitable for the family, the two people came to Hangzhou to develop together after graduation. After two years of hard work, in last July, we finally ended our five-year love long run. we witnessed our love with friends and friends on the West Lake. ', 'Category' => 'weekly Select', 'utc _ timestemp' => time (); $ o-> addItems ($ aItem ); $ aItem = array ('title' => 'to meet space requirements, it is okay to buy a second-hand MPV! ', 'Link' => 'http: // www.chemao.com.cn/javaske/esmpv.html', 'description' =>' with the policy of the second child and the whole peopleEntrepreneurshipMore and more MPVs are running on the road. the powerful use of these models has put a lot of people at ease. The arrival of November 10 not only increases the discount for new cars, but also decreases the price of second-hand cars. The price of MPV is "easy and easy". it is a great time for netizens who want to start with such models! Below, we recommend several popular MPVs in the second-hand car market. ', 'Category' => 'weekly Select', 'utc _ timestemp' => time (); $ o-> addItems ($ aItem ); echo $ o-> getSerialize ();

    The above introduces the RSS 20 php generation class. the quick-read generation requires an xml file, including the entrepreneurial content. I hope my friends who are interested in PHP tutorials can help me.

    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.