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

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)             $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                         $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 までご連絡ください。
    php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

    php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

    php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

    实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

    php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

    php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

    php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

    判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

    php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

    方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

    php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

    php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

    php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

    在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

    php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

    php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

    See all articles

    ホット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ヘンタイを無料で生成します。

    ホットツール

    ゼンドスタジオ 13.0.1

    ゼンドスタジオ 13.0.1

    強力な PHP 統合開発環境

    SublimeText3 中国語版

    SublimeText3 中国語版

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

    SublimeText3 Linux 新バージョン

    SublimeText3 Linux 新バージョン

    SublimeText3 Linux 最新バージョン

    メモ帳++7.3.1

    メモ帳++7.3.1

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

    ドリームウィーバー CS6

    ドリームウィーバー CS6

    ビジュアル Web 開発ツール