Example of php making xml-based RSS feed function

高洛峰
Release: 2023-03-05 17:02:01
Original
1160 people have browsed it

This article mainly introduces the function of PHP to create RSS feeds based on xml, and analyzes the definition and use of RSS feed file generation classes in the form of examples. Friends in need can refer to the examples of this article

Describes the function of using PHP to create RSS feeds based on XML. Share it with everyone for your reference, the details are as follows:

First make an RSS template. The file name of the template is feed.xml, and the code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>
Copy after login

Then use a php file 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 = &#39;./feed.xml&#39;;
  protected $rss = null;
  protected $title = &#39;&#39;;
  protected $desc = &#39;&#39;;
  protected $link = &#39;&#39;;
  public function __construct(){
    $this->title = &#39;物理学&#39;;
    $this->desc = &#39;现代物理学&#39;;
    $this->link = &#39;http://mysql/rss.php&#39;;
    $this->dom = new DOMDocument(&#39;1.0&#39;,&#39;utf-8&#39;);
    $this->dom->load($this->temp);
    $this->rss = $this->dom->getElementsByTagName(&#39;rss&#39;)->item(0);
  }
  public function feed($arr){
    $this->createChannel();
    $channel = $this->dom->getElementsByTagName(&#39;channel&#39;)->item(0);
    foreach ($arr as $v){
      $channel->appendChild($this->createItem($v));
    }
    header(&#39;content-type:text/xml&#39;);
    echo $this->dom->savexml();
  }
  protected function createChannel(){
    $channel = $this->dom->createElement(&#39;channel&#39;);
    $channel->appendChild($this->createEle(&#39;title&#39;,$this->title));
    $channel->appendChild($this->createEle(&#39;link&#39;,$this->link));
    $channel->appendChild($this->createEle(&#39;description&#39;,$this->desc));
    $this->rss->appendChild($channel);
  }
  protected function createItem($arr){
    $item = $this->dom->createElement(&#39;item&#39;);
    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(
  array(
    &#39;title&#39;=>&#39;牛顿力学&#39;,
    &#39;link&#39;=>&#39;1&#39;,
    &#39;description&#39;=>&#39;牛顿力学&#39;
  ),
  array(
    &#39;title&#39;=>&#39;相对论&#39;,
    &#39;link&#39;=>&#39;1&#39;,
    &#39;description&#39;=>&#39;爱因斯坦的相对论&#39;
  )
);
$rss = new Rss;
$rss->feed($arr);
?>
Copy after login

Finally The effect under Firefox:

Example of php making xml-based RSS feed function

For more PHP production of xml-based RSS feed function examples and related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template