PHP implementation of XML-based RSS Feed function method

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


Related Article

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.