This article mainly introduces the xml-based RSS feed generation function of php, and analyzes the definition and usage of the RSS feed source file generation class based on the instance form, for more information about how to create an xml-based RSS feed in php, see the next article. This article analyzes the definition and usage of the class for generating RSS feed source files in the form of examples, for more information, see
This article describes how to use php to create an xml-based RSS feed. We will share this with you for your reference. The details are as follows:
First, create an RSS template. the template file name is feed. xml. the code is as follows:
In addition, the PHP file is used to read data from the database and generate an RSS file. here, an array is used to simulate the data read from the database:
Title = 'physicist '; $ this-> desc = 'Modern physicists'; $ this-> link =' http://mysql/rss.php '; $ This-> dom = new DOMDocument (' 1. 0', 'utf-8'); $ this-> dom-> load ($ this-> temp ); $ this-> rss = $ this-> dom-> getElementsByTagName ('RSS ')-> item (0);} public function feed ($ arr) {$ this-> createChannel (); $ channel = $ this-> dom-> getElementsByTagName ('channel')-> item (0); foreach ($ arr as $ v) {$ channel-> appendChild ($ this-> createItem ($ v);} header ('content-type: text/XML '); echo $ this-> dom-> savexml ();} protect Ed function createChannel () {$ channel = $ this-> dom-> createElement ('channel'); $ channel-> appendChild ($ this-> createEle ('title ', $ this-> title); $ channel-> appendChild ($ this-> createEle ('link', $ this-> link )); $ channel-> appendChild ($ this-> createEle ('Description', $ this-> desc); $ this-> rss-> appendChild ($ channel );} protected function createItem ($ arr) {$ item = $ this-> dom-> createElement ('ITEM'); foreach ($ arr As $ k =>$ v) {$ item-> appendChild ($ this-> createEle ($ k, $ v);} return $ item ;} protected function createEle ($ name, $ value) {$ e = $ this-> dom-> createElement ($ name ); $ t = $ this-> dom-> createTextNode ($ value); $ e-> appendChild ($ t); return $ e ;}} $ arr = array ('title' => 'Newton mechanic ', 'link' => '1', 'description' => 'Newton mechanic '), array ('title' => 'Relativity ', 'link' => '1', 'description' => 'Einstein's relativity'); $ rss = new Rss; $ rss-> feed ($ Rr);?>
Finally, the effect of Firefox is as follows:
For more articles about how to create an xml-based RSS feed source function example in php, refer to the PHP Chinese website!