Use php to create an xml-based RSS feed source function example and an xmlrss subscription example

Source: Internet
Author: User

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

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.