Use php to create an xml-based RSS feed source function example and an xmlrss subscription example
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:
<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>
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:
<? Php class Rss {protected $ dom = null; protected $ temp = '. /feed. xml '; protected $ rss = null; protected $ title = ''; protected $ desc =''; protected $ link = ''; public function _ construct () {$ this-> title = 'physicist '; $ this-> desc = 'modern physicist'; $ 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 ();} protected function createChannel () {$ channel = $ this-> dom-> createElement ('channel '); $ channel-> appendChild ($ this-> createEle ('title', $ thi S-> 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 Theory of Relativity '); $ rss = new Rss; $ rss-> feed ($ arr);?>
Finally, the effect of Firefox is as follows:
PS: Here are some online tools for xml operations for your reference:
Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson
Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat
XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress
XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat