Example of using php to create an xml-based RSS feed source

Source: Internet
Author: User
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!

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.