目次
PHPでRSSサブスクリプションを生成する方法、phprssサブスクリプション
ホームページ バックエンド開発 PHPチュートリアル php で RSS サブスクリプションを生成する方法、phprss subscription_PHP チュートリアル

php で RSS サブスクリプションを生成する方法、phprss subscription_PHP チュートリアル

Jul 13, 2016 am 10:06 AM
php rss

PHPでRSSサブスクリプションを生成する方法、phprssサブスクリプション

この記事の例では、php で RSS サブスクリプションを生成する方法を説明します。参考のためにみんなで共有してください。具体的な分析は次のとおりです:

RSS (Really Simple Syndication、シンジケート コンテンツとも呼ばれる) は、Web サイトのコンテンツを記述および同期するための形式です。 RSS は、次の 3 つの解釈のいずれかになります: Really Simple Syndication、RDF (リソース記述フレームワーク) サイトの概要。しかし実際には、これら 3 つの説明はすべて同じシンジケーション テクノロジーについて言及しています。 RSS は現在、オンライン ニュース チャネル、ブログ、Wiki で広く使用されています。 RSS 購読を使用すると、Web サイトで情報をより迅速に取得できるため、ユーザーは Web サイトのコンテンツの最新の更新を取得するのに役立ちます。ネットワーク利用者は、クライアント上でRSSに対応したアグリゲーションツールソフトを利用することで、Webサイトのコンテンツページを開かずにRSS出力に対応したWebサイトのコンテンツを閲覧できます。
技術的に言えば、RSS ファイルは標準化された XML データであり、通常、ファイルには rss、xml、または rdf が接尾辞として付けられます。以下は、rss ファイルの内容の例です。

コードをコピーします コードは次のとおりです:
<チャンネル> バンケの家 <リンク>http://www.bkjia.com/ バンケジージャ
RSS チュートリアル ウェブサイトアドレス/rss W3School の新しい RSS チュートリアル
XML チュートリアル ウェブサイトのアドレス/xml W3School の新しい XML チュートリアル


以下は、php を使用して RSS を動的に生成するコード例です。


コードをコピーします

コードは次のとおりです:
/**
** PHP は RSS クラスを動的に生成します
**/
定義("タイムゾーン","");
define("FEEDCREATOR_VERSION","www.jb51.net");//您的网址
class FeedItem extends HtmlDescribable{
    var $title,$description,$link;
    var $author,$authorEmail,$image,$category,$comments,$guid,$source,$creator;
    var $date;
    var $AdditionalElements=Array();
}
 
class FeedImage extends HtmlDescribable{
    var $title,$url,$link;
    var $width,$height,$description;
}
 
class HtmlDescribable{
    var $descriptionHtmlSyndicated;
    var $descriptionTruncSize;
 
    関数 getDescription(){
        $descriptionField=new FeedHtmlField($this->description);
        $descriptionField->syndicateHtml=$this->descriptionHtmlSyndicated;
        $descriptionField->truncSize=$this->descriptionTruncSize;
        return $descriptionField->output();
    }
}
 
クラス FeedHtmlField{
    var $rawFieldContent;
    var $truncSize,$syndicateHtml;
    関数 FeedHtmlField($parFieldContent){
        if($parFieldContent){
            $this->rawFieldContent=$parFieldContent;
        }
    }
    関数出力(){
        if(!$this->rawFieldContent){
            $結果="";
        } elseif($this->syndicateHtml){
            $result="rawFieldContent."]]>";
        }その他{
            if($this->truncSize と is_int($this->truncSize)){
                $result=FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
            }その他{
                $result=htmlspecialchars($this->rawFieldContent);
            }
        }
        $result を返します。
    }
}
 
class UniversalFeedCreator extends FeedCreator{
    var $_feed;
    関数 _setFormat($format){
        スイッチ (strtoupper($format)){
            ケース「2.0」:
                // 失敗します
            ケース「RSS2.0」:
                $this->_feed=new RSSCreator20();
                壊す;
            ケース「0.91」:
                // 失敗します
            ケース「RSS0.91」:
                $this->_feed=new RSSCreator091();
                壊す;
            デフォルト:
                $this->_feed=new RSSCreator091();
                壊す;
        }
        $vars=get_object_vars($this);
        foreach ($vars as $key => $value){
            // プロパティ "contentType","encoding" の上書きを防止します。 「_feed」自体をコピーしないでください
            if(!in_array($key, array("_feed","contentType","encoding"))){
                $this->_feed->{$key}=$this->{$key};
            }
        }
    }
 
    関数 createFeed($format="RSS0.91"){
        $this->_setFormat($format);
        return $this->_feed->createFeed();
    }
 
    function saveFeed($format="RSS0.91",$filename="",$displayContents=true){
        $this->_setFormat($format);
        $this->_feed->saveFeed($filename,$displayContents);
    }
 
    関数 useCached($format="RSS0.91",$filename="",$timeout=3600){
        $this->_setFormat($format);
        $this->_feed->useCached($filename,$timeout);
    }
}
 
class FeedCreator extends HtmlDescribable{
    var $title,$description,$link;
    var $syndicationURL,$image,$ language,$copyright,$pubDate,$lastBuildDate,$editor,$editorEmail,$webmaster,$category,$docs,$ttl,$評価,$skipHours,$skipDays;
    var $xslStyleSheet="";
    var $items=Array();
    var $contentType="アプリケーション/xml";
    var $encoding="utf-8";
    var $AdditionalElements=Array();
 
    関数 addItem($item){
        $this->items[]=$item;
    }
 
    関数clearItem2Null(){
        $this->items=array();
    }
 
    関数 iTrunc($string,$length){
        if(strlen($string)             $string を返します。
        }
 
        $pos=strrpos($string,".");
        if($pos>=$length-4){
            $string=substr($string,0,$length-4);
            $pos=strrpos($string,".");
        }
        if($pos>=$length*0.4){
            return substr($string,0,$pos+1)." ...";
        }
 
        $pos=strrpos($string," ");
        if($pos>=$length-4){
            $string=substr($string,0,$length-4);
            $pos=strrpos($string," ");
        }
        if($pos>=$length*0.4){
            return substr($string,0,$pos)." ...";
        }
 
        substr($string,0,$length-4) を返します。" ...";
    }
 
    関数 _createGeneratorComment(){
        return "n";
    }
 
    function _createAdditionalElements($elements,$indentString=""){
        $ae="";
        if(is_array($elements)){
            foreach($elements AS $key => $value){
                $ae.= $indentString."$value$key>n";
            }
        }
        $aeを返します;
    }
 
    関数 _createStylesheetReferences(){
        $xml="";
        if($this->cssStyleSheet) $xml .= "cssStyleSheet."" type="text/css"?>n";
        if($this->xslStyleSheet) $xml .= "xslStyleSheet."" type="text/xsl"?>n";
        $xml を返します。
    }
 
    関数 createFeed(){}
 
    関数 _generateFilename(){
        $fileInfo=pathinfo($_SERVER["PHP_SELF"]);
        return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";
    }
 
    関数 _redirect($filename){
        Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename));
        Header("Content-Disposition: inline; filename=".basename($filename));
        readfile($filename,"r");
        死ぬ();
    }
 
    関数 useCached($filename="",$timeout=3600){
        $this->_timeout=$timeout;
        if($filename==""){
            $filename=$this->_generateFilename();
        }
        if(file_exists($filename) && (time()-filemtime($filename) < $timeout)){
            $this->_redirect($filename);
        }
    }
 
    関数 saveFeed($filename="",$displayContents=true){
        if($filename==""){
            $filename=$this->generateFilename();
        }
        $feedFile=fopen($filename,"w+");
        if($feedFile){
            fputs($feedFile,$this->createFeed());
            fclose($feedFile);
            if($displayContents){
                $this->_redirect($filename);
            }
        }その他{
            echo "
フィード ファイルの作成中にエラーが発生しました。書き込み権限を確認してください。
";
        }
    }
}
 
クラス FeedDate{
    var $unix;
    関数 FeedDate($dateString=""){
        if($dateString=="") $dateString=date("r");
        if(is_integer($dateString)){
            $this->unix=$dateString;
            戻る;
        }
        if(preg_match("~(?:(?:月|火|水|木|金|土|日),\s+)?(\d{1,2})\s+([a-zA-Z] {3})\s+(\d{4})\s+(\d{2}):(\d{2}):(\d{2})\s+(.*)~",$dateString, $matches)){
            $months=Array("1月"=>1,"2月"=>2,"3月"=>3,"4月"=>4,"5月"=>5,"6月"=> ;6、「7月」=>7、「8月」=>8、「9月」=>9、「10月」=>10、「11月」=>11、「12月」=>12 );
            $this->unix=mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
            if(substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'){
                $tzOffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
            }その他{
                if(strlen($matches[7])==1){
                    $1時間=3600;
                    $ord=ord($matches[7]);
                    if($ord < ord("M")){
                        $tzOffset=(ord("A") - $ord - 1) * $oneHour;
                    } elseif($ord >= ord("M") && $matches[7]!="Z"){
                        $tzOffset=($ord - ord("M")) * $oneHour;
                    elseif($matches[7]=="Z"){
                        $tzOffset=0;
                    }
                }
                スイッチ ($matches[7]){
                    ケース「UT」:
                    ケース「GMT」: $tzOffset=0;
                }
            }
            $this->unix += $tzOffset;
            戻る;
        }
        if(preg_match("~(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{ 2})(.*)~",$dateString,$matches)){
            $this->unix=mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
            if(substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'){
                $tzOffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
            }その他{
                if($matches[7]=="Z"){
                    $tzOffset=0;
                }
            }
            $this->unix += $tzOffset;
            戻る;
        }
        $this->unix=0;
    }
 
    関数 rfc822(){
        $date=gmdate("Y-m-d H:i:s",$this->unix);
        if(TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE);
        $date を返します。
    }
 
    関数 iso8601(){
        $date=gmdate("Y-m-d H:i:s",$this->unix);
        $date=substr($date,0,22) 。 ':' 。 substr($date,-2);
        if(TIME_ZONE!="") $date=str_replace("+00:00",TIME_ZONE,$date);
        $date を返します。
    }
 
    関数 unix(){
        $this->unix を返す;
    }
}
 
クラス RSSCreator10 は FeedCreator を拡張します{
    関数 createFeed(){
        $feed="encoding.""?>n";
        $feed.= $this->_createGeneratorComment();
        if($this->cssStyleSheet==""){
            $cssStyleSheet="http://www.w3.org/2000/08/w3c-synd/style.css";
        }
        $feed.= $this->_createStylesheetReferences();
        $feed.= "         $feed.= " xmlns="http://purl.org/rss/1.0/"n";
        $feed.= " xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"n";
        $feed.= " xmlns:slash="http://purl.org/rss/1.0/modules/slash/"n";
        $feed.= " xmlns:dc="http://purl.org/dc/elements/1.1/">n";
        $feed.= " syndicationURL."">n";
        $feed.= " ".htmlspecialchars($this->title)."n";
        $feed.= " ".htmlspecialchars($this->description)."n";
        $feed.= " ".$this->link."n";
        if($this->image!=null){
            $feed.= " image->url."" />n";
        }
        $now=新しいフィード日付();
        $feed.= " ".htmlspecialchars($now->iso8601())."n";
        $feed.= " <アイテム>n";
        $feed.= " n";
        for ($i=0;$iitems);$i++){
            $feed.= " items[$i]->link).""/>n";
        }
        $feed.= "
n";
        $feed.= " n";
        $feed.= "
n";
        if($this->image!=null){
            $feed.= " image->url."">n";
            $feed.= " ".$this->image->title."n";
            $feed.= " ".$this->image->link."n";
            $feed.= " ".$this->image->url."n";
            $feed.= " n";
        }
        $feed.= $this->_createAdditionalElements($this->AdditionalElements," ");
 
        for ($i=0;$iitems);$i++){
            $feed.= " items[$i]->link)."">n";
            //$feed.= " 投稿n";
            $feed.= " text/htmln";
            if($this->items[$i]->date!=null){
                $itemDate=new FeedDate($this->items[$i]->date);
                $feed.= " ".htmlspecialchars($itemDate->iso8601())."n";
            }
            if($this->items[$i]->source!=""){
                $feed.= " ".htmlspecialchars($this->items[$i]->source)."n";
            }
            if($this->items[$i]->author!=""){
                $feed.= " ".htmlspecialchars($this->items[$i]->author)."n";
            }
            $feed.= " ".htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"nr"," ")))."n" ;
            $feed.= " ".htmlspecialchars($this->items[$i]->link)."n";
            $feed.= " ".htmlspecialchars($this->items[$i]->description)."n";
            $feed.= $this->_createAdditionalElements($this->items[$i]->AdditionalElements," ");
            $feed.= "
n";
        }
        $feed.= "n";
        $feed を返します。
    }
}
 
クラス RSSCreator091 は FeedCreator を拡張します{
    var $RSSVersion;
 
    関数 RSSCreator091(){
        $this->_setRSSVersion("0.91");
        $this->contentType="アプリケーション/rss+xml";
    }
 
    function _setRSSVersion($version){
        $this->RSSVersion=$version;
    }
 
    関数 createFeed(){
        $feed="encoding.""?>n";
        $feed.= $this->_createGeneratorComment();
        $feed.= $this->_createStylesheetReferences();
        $feed.= "RSSVersion."">n";
        $feed.= " <チャンネル>n";
        $feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."n";
        $this->descriptionTruncSize=500;
        $feed.= " ".$this->getDescription()."n";
        $feed.= " ".$this->link."n";
        $now=新しいフィード日付();
        $feed.= " ".htmlspecialchars($now->rfc822())."n";
        $feed.= " ".FEEDCREATOR_VERSION."n";
 
        if($this->image!=null){
            $feed.= " <画像>n";
            $feed.= " ".$this->image->url."n";
            $feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->image->title),100)."n";
            $feed.= " ".$this->image->link."n";
            if($this->image->width!=""){
                $feed.= " ".$this->image->width."n";
            }
            if($this->image->height!=""){
                $feed.= " <高さ>".$this->image->height."高さ>n";
            }
            if($this->image->description!=""){
                $feed.= " ".$this->image->getDescription()."n";
            }
            $feed.= " n";
        }
        if($this->言語!=""){
            $feed.= " <言語>".$this->言語."n";
        }
        if($this->copyright!=""){
            $feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."n";
        }
        if($this->エディタ!=""){
            $feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."n";
        }
        if($this->ウェブマスター!=""){
            $feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."n";
        }
        if($this->pubDate!=""){
            $pubDate=new FeedDate($this->pubDate);
            $feed.= " ".htmlspecialchars($pubDate->rfc822())."n";
        }
        if($this->category!=""){
            $feed.= " ".htmlspecialchars($this->category)."n";
        }
        if($this->docs!=""){
            $feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."n";
        }
        if($this->ttl!=""){
            $feed.= " ".htmlspecialchars($this->ttl)."n";
        }
        if($this->評価!=""){
            $feed.= " <評価>".FeedCreator::iTrunc(htmlspecialchars($this->評価),500)."n";
        }
        if($this->skipHours!=""){
            $feed.= " ".htmlspecialchars($this->skipHours)."n";
        }
        if($this->skipDays!=""){
            $feed.= " ".htmlspecialchars($this->skipDays)."n";
        }
        $feed.= $this->_createAdditionalElements($this->AdditionalElements," ");
 
        for ($i=0;$iitems);$i++){
            $feed.= " <アイテム>n";
            $feed.= " ".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."n";
            $feed.= " ".htmlspecialchars($this->items[$i]->link)."n";
            $feed.= " ".$this->items[$i]->getDescription()."n";
 
            if($this->items[$i]->author!=""){
                $feed.= " ".htmlspecialchars($this->items[$i]->author)."n";
            }
            /*
             // 保留中
             if($this->items[$i]->source!=""){
             $feed.= " ".htmlspecialchars($this->items[$i]->source)."n";
             }
             */
            if($this->アイテム[$i]->カテゴリ!=""){
                $feed.= " ".htmlspecialchars($this->items[$i]->category)."n";
            }
            if($this->アイテム[$i]->コメント!=""){
                $feed.= " ".htmlspecialchars($this->items[$i]->comments)."n";
            }
            if($this->items[$i]->date!=""){
                $itemDate=new FeedDate($this->items[$i]->date);
                $feed.= " ".htmlspecialchars($itemDate->rfc822())."n";
            }
            if($this->items[$i]->guid!=""){
                $feed.= " ".htmlspecialchars($this->items[$i]->guid)."n";
            }
            $feed.= $this->_createAdditionalElements($this->items[$i]->AdditionalElements," ");
            $feed.= " n";
        }
        $feed.= " n";
        $feed.= "
n";
        $feed を返します。
    }
}
 
クラス RSSCreator20 は RSSCreator091 を拡張します{
 
    関数 RSSCreator20(){
        親::_setRSSVersion("2.0");
    }
}

使用例:
复制代码代码如下:
header('Content-Type:text/html; charset=utf-8'); $db=mysql_connect('127.0.0.1','root','123456'); mysql_query("セット名 utf8"); mysql_select_db('dbname',$db); $brs=mysql_query('select * 記事の順序から add_time 記述制限 0,10',$db); $rss=新しいユニバーサルフィードクリエイター(); $rss->title="ページタイトル"; $rss->link="ウェブサイト http://"; $rss->description="rss タイトル"; while($rowbrs=mysql_fetch_array($brs)){
$item=新しいフィードアイテム(); $item->title =$rowbrs['件名']; $item->link='http://www.bkjia.com/'; $item->説明 =$rowbrs['説明']
$rss->addItem($item); }
mysql_close($db); $rss->saveFeed("RSS2.0","rss.xml");


この記事で説明した内容が皆様の PHP プログラミング設計に役立つことを願っています。




http://www.bkjia.com/PHPjc/958257.html

www.bkjia.com

tru​​e

http://www.bkjia.com/PHPjc/958257.html
技術記事

php、phprss サブスクリプションで RSS サブスクリプションを生成する方法 この記事では、php で RSS サブスクリプションを生成する方法について説明します。参考のためにみんなで共有してください。具体的な分析は次のとおりです: RSS (Really Simple Information Syndication、別名...

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Dec 24, 2024 pm 04:42 PM

PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP の日付と時刻 CakePHP の日付と時刻 Sep 10, 2024 pm 05:27 PM

Cakephp4 で日付と時刻を操作するには、利用可能な FrozenTime クラスを利用します。

CakePHP ファイルのアップロード CakePHP ファイルのアップロード Sep 10, 2024 pm 05:27 PM

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP について話し合う CakePHP について話し合う Sep 10, 2024 pm 05:28 PM

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP バリデータの作成 CakePHP バリデータの作成 Sep 10, 2024 pm 05:26 PM

Validator は、コントローラーに次の 2 行を追加することで作成できます。

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 Dec 20, 2024 am 11:31 AM

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

CakePHP クイックガイド CakePHP クイックガイド Sep 10, 2024 pm 05:27 PM

CakePHP はオープンソースの MVC フレームワークです。これにより、アプリケーションの開発、展開、保守がはるかに簡単になります。 CakePHP には、最も一般的なタスクの過負荷を軽減するためのライブラリが多数あります。

PHPでHTML/XMLを解析および処理するにはどうすればよいですか? PHPでHTML/XMLを解析および処理するにはどうすればよいですか? Feb 07, 2025 am 11:57 AM

このチュートリアルでは、PHPを使用してXMLドキュメントを効率的に処理する方法を示しています。 XML(拡張可能なマークアップ言語)は、人間の読みやすさとマシン解析の両方に合わせて設計された多用途のテキストベースのマークアップ言語です。一般的にデータストレージに使用されます

See all articles