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 ファイルの内容の例です。
以下は、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: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.= "
$feed.= "
$feed.= "
$feed.= " ".$this->link."n";
if($this->image!=null){
$feed.= "
}
$now=新しいフィード日付();
$feed.= "
$feed.= " n";
$feed.= "
for ($i=0;$i
$feed.= "
}
$feed.= "
$feed.= " n";
$feed.= "
if($this->image!=null){
$feed.= "
$feed.= "
$feed.= " ".$this->image->link."n";
$feed.= "
$feed.= "
}
$feed.= $this->_createAdditionalElements($this->AdditionalElements," ");
for ($i=0;$i
$feed.= "
//$feed.= "
$feed.= "
if($this->items[$i]->date!=null){
$itemDate=new FeedDate($this->items[$i]->date);
$feed.= "
}
if($this->items[$i]->source!=""){
$feed.= "
}
if($this->items[$i]->author!=""){
$feed.= "
}
$feed.= "
$feed.= " ".htmlspecialchars($this->items[$i]->link)."n";
$feed.= "
$feed.= $this->_createAdditionalElements($this->items[$i]->AdditionalElements," ");
$feed.= "
}
$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.= "
$feed.= " n";
$feed.= "
$this->descriptionTruncSize=500;
$feed.= "
$feed.= " ".$this->link."n";
$now=新しいフィード日付();
$feed.= "
$feed.= "
if($this->image!=null){
$feed.= " n";
$feed.= "
$feed.= "
$feed.= " ".$this->image->link."n";
if($this->image->width!=""){
$feed.= "
}
if($this->image->height!=""){
$feed.= " ".$this->image->height."高さ>n";
}
if($this->image->description!=""){
$feed.= "
}
$feed.= " n";
}
if($this->言語!=""){
$feed.= " ".$this->言語."言語>n";
}
if($this->copyright!=""){
$feed.= "
}
if($this->エディタ!=""){
$feed.= "
}
if($this->ウェブマスター!=""){
$feed.= "
}
if($this->pubDate!=""){
$pubDate=new FeedDate($this->pubDate);
$feed.= "
}
if($this->category!=""){
$feed.= "
}
if($this->docs!=""){
$feed.= "
}
if($this->ttl!=""){
$feed.= "
}
if($this->評価!=""){
$feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->評価),500)."評価>n";
}
if($this->skipHours!=""){
$feed.= "
}
if($this->skipDays!=""){
$feed.= "
}
$feed.= $this->_createAdditionalElements($this->AdditionalElements," ");
for ($i=0;$i
$feed.= " n";
$feed.= "
$feed.= " ".htmlspecialchars($this->items[$i]->link)."n";
$feed.= "
if($this->items[$i]->author!=""){
$feed.= "
}
/*
// 保留中
if($this->items[$i]->source!=""){
$feed.= "
}
*/
if($this->アイテム[$i]->カテゴリ!=""){
$feed.= "
}
if($this->アイテム[$i]->コメント!=""){
$feed.= "
}
if($this->items[$i]->date!=""){
$itemDate=new FeedDate($this->items[$i]->date);
$feed.= "
}
if($this->items[$i]->guid!=""){
$feed.= "
}
$feed.= $this->_createAdditionalElements($this->items[$i]->AdditionalElements," ");
$feed.= " n";
}
$feed.= " n";
$feed.= "
$feed を返します。
}
}
クラス RSSCreator20 は RSSCreator091 を拡張します{
関数 RSSCreator20(){
親::_setRSSVersion("2.0");
}
}
使用例:
$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
true
http://www.bkjia.com/PHPjc/958257.html

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

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

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

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

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

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

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

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


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

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

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

メモ帳++7.3.1
使いやすく無料のコードエディター

ドリームウィーバー CS6
ビジュアル Web 開発ツール
