This article mainly introduces the PHP production of XML-based RSS feed function, combined with the example form analysis of RSS feed file generation class definition and use method, the need for friends can refer to the following
The example of this article is about PHP's XML-based RSS feed functionality. Share to everyone for your reference, as follows:
First make an RSS template, the filename of the template 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>
And then using PHP files to read from the database and generate an RSS file, here is an array 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 = ' Physics '; $this->desc = ' modern Physics '; $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 ', $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 mechanics ', ' link ' = ' 1 ', ' description ' = ' Newton Mechanics '), Array (' title ' = ' = ') Relativity ', ' link ' = ' 1 ', ' description ' and ' Einstein's theory of relativity '); $rss = new RSS; $rss->feed ($arr); >
Finally in Firefox effect:
The above is the whole content of this article, I hope that everyone's study has helped.