XML を使用して RSS 購読を実装する

怪我咯
リリース: 2017-04-05 17:27:08
オリジナル
2454 人が閲覧しました

RSS は Web コンテンツのシンジケーション形式であり、XML の一種です。すべての RSS ドキュメントは XML1.0 仕様に従います。

具体例:

<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version = "1.0" encoding = "utf-8" ?>  
<rss version = "2.0" xmlns:wfw ="http://wellformedweb.org/CommentAPI/">  
       <channel>  
              <title>标题</title>  
              <link>链接地址</link>  
              <description>描述</description>  
              <language>描述语言</language>  
              <copyright>版本</copyright>  
              <pubdate>时间</pubdate>  
              <item>  
                     <title>日志标题</title>  
                  <link>日志的url访问地址</link>  
                     <author>日志的作者</author>  
                     <pubdate>日志的发布时间</pubdate>  
                     <description>日志的内容</description>  
              </item>  
       </channel>  
</rss></span>
ログイン後にコピー

この時点で、データベースに接続し、必要な結果を出力する必要があります。

Test.PHP

<span style="font-family:Microsoft YaHei;font-size:18px;"><?PHP  
       include("./conn.php");  
       class test{  
              public $title = &#39;&#39;;  
              public $link = &#39;&#39;;  
              public $description= &#39;&#39;;  
              public $items = &#39;&#39;;  
              public $template =&#39;./test.xml&#39;;  
              public $dom = &#39;&#39;;  
              public $rss = &#39;&#39;;  
              public function__construct(){  
                     $this ->dom = new domDocument(&#39;1.0&#39;,&#39;utf-8&#39;);  
                     $this ->dom -> load($this -> template);  
                     $this ->rss = $this -> dom -> GetElementsByTagName(&#39;rss&#39;);  
              }  
              public functioncreateChannel(){  
                     $channel =$this -> dom -> createElement("channel");  
                     $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 ->description));  
                     $this ->rss -> appendChild($channel);  
                        
              }  
              public functioncreateEle($name,$value){  
                     $element =$this -> dom -> createElement($name);  
                     $text = $this-> dom -> createTextNode($value);  
                     $element-> appendChild($text);  
                     return$element;  
              }  
              protected functionadditem($list){  
                     foreach($listas $goods){  
                            $this-> rss -> appendChild($this-> createitem($goods));  
                     }  
              }  
              public functioncreateitem($arr){  
                     $item = $this-> dom -> createElement("item");  
                     foreach($arras $key => $value){  
                            $item-> appendChild($this -> createEle($key,$value));  
                     }  
                     return $item;  
              }  
                 
              public functiondisplay(){  
                     $this ->createChannel();  
                     $this ->additem($this -> items);  
                     echo $this-> dom -> savexml();  
              }  
       }  
       $sql = "select * fromstu";  
       $rs = mysql_query($sql);  
       while($row =mysql_fetch_assoc($rs)){  
              $list[] = $row;  
       }  
       $test = new test();  
       $test -> title = "测试标题";  
       $test -> link = "测试连接";  
       $test -> description ="测试内容";  
       $test -> display();  
       $test -> items = $list;  
</span>
ログイン後にコピー


以上がXML を使用して RSS 購読を実装するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!