ホームページ  >  記事  >  バックエンド開発  >  PHP 完璧な RSS 生成クラス_PHP チュートリアル

PHP 完璧な RSS 生成クラス_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-20 11:10:51819ブラウズ

RSS 購読機能は多くの Web サイトで見つかりますが、次のコードは私自身が作成したものであり、その中で PHP クラスが使用されています。非常に便利なので、あえて使用しません。自分だけの秘密にしておきたいので、みんなにシェアします。

コードは次のとおりです コードをコピーします

include_once("class/RSS.class.php");//RSS PHPクラスの紹介
$RSS= new RSS("Name","アドレス","説明","RSS チャンネル アイコン");
$RSS->AddItem("投稿のタイトル", "投稿のアドレス", "投稿の概要", "投稿の公開日" );
$RSS->Display ();//RSSコンテンツを出力


すべてのコードは次のとおりです:

代码如下 复制代码
// +---------------------------- --------------------------------------
// | Yブログ
// +------------------------------------------ -------------------------
// |著作権 (c) 2008 http://www.hzhuti.com/nokia/n97/ 全著作権所有
// +---------------------- -----------------------------------------------
// +------------------------------------------------ ---------------------
// |作者: yhustc
// +------------------------------------- -----------------------------------
// $Id$

/**
+------------------------------------------------ ----------------------------------
* RSS生成クラス
+----------- ---------------------------------------------------- ---- ---------------
* @author yhustc
* @version $Id$
+--------- ---------------------------------------------------- ---- ---------------
* /
class RSS
{
/**
+------------------------------------------------ ----------
* RSS チャンネル名
+--------------------------------- - ----------------------------------
* @var string
* @access protected
+--- ---------- -------------------------------------- --------
*/
protected $channel_title = "';
/**
+------------------------------------------------ ----------
* RSS チャンネルリンク
------------------------
* @var string
* @アクセスが保護されています
+--------------- ----------------------------- ------------
*/
protected $channel_link = "';
/**
+------------------------------------------------ ----------
* RSS チャンネルの説明
----------
* @var string
* @アクセスが保護されています
+--------------- ----------------------------- ------------
*/
protected $channel_description = '';
/**
+------------------------------------------------ ----------
* RSS チャンネルで使用される小さいアイコンの URL
* +------------------------ ------- ----------------------------------
* @var string
* @access保護中
+-------- -------------------------------------- -----------
*/
protected $channel_imgurl = '';
/**
+------------------------------------------------ ----------
* RSS チャネルで使用される言語
----------------------------
* @var string
* @access protected
+---------- ---------------------------- ----------------
*/
protected $ language = 'zh_CN';
/**
+------------------------------------------------ ----------
* * RSS ドキュメントの作成日、デフォルトは今日です
* +---------------------- --- ----------------------------------
* @var string
* @access protected
+- ---------------------------------------------------- -------
* /
protected $pubDate = '';
protected $lastBuildDate = '';

protected $generator = 'YBlog RSS Generator';

/**
+-------------- ------------------------------------------
     * RSS单条情報の数组
+------------------------------------------ ----------------
* @var string
* @access protected
+---------------------- ------------------------------------
*/
protected $items = array();

/**
+------------------------------------------------ ----------
* コンストラクター
+---------------------------------- - -------------------------
* @access public
+--------------- - --------------------------------------
* @param string $title RSS チャンネル名
* @param string $link RSS チャンネルのリンク
* @param string $description RSS チャンネルの説明
* @param string $imgurl RSS チャンネルのアイコン
+------------------ -------------------------------------------------------- ---
*/
public function __construct($title, $link, $description, $imgurl = '')
{
$this->channel_title = $title;
$this->channel_link = $ link;
$this->channel_description = $description;
$this->channel_imgurl = $imgurl;
$this->pubDate = Date('Y-m-d H:i:s', time());
$ this->lastBuildDate = Date('Y-m-d H:i:s', time());
}

/**
+------------------------------------------------ ----------
*プライベート変数を設定します
------------------------
* @access public
+- ----------------- --------------------------------- -------
* @param string $key 変数名
* @param string $value 変数の値
+----------- ------------------- ------------------
*/
public function Config($key,$value)
{
$this ->{$key} = $value;
}

/**
+------------------------------------------------ ----------
* RSS アイテムを追加
------------------------
* @access public
+- ----------------- --------------------------------- -------
* @param string $title ログのタイトル
* @param string $link ログのリンク
* @param string $description ログの概要
* @param string $pubDateログの発行日
--------------------------------------------------------
*/
function AddItem($title, $link, $description, $pubDate)
{
$this->items[] = array('title' => $title, 'link' => $link, 'description' => $description, 'pubDate' => $pubDate);
}

/**
+------------------------------------------------ ----------
* RSS XML を文字列として出力します
* +---------------------------- --- --------------------------------
* @access public
+-------- ------ -------------------------------------------- ------
* @return string
+------------------------------------- ------------ --------
*/
public function Fetch()
{
$rss = "rn";
$rss = "rn";
        $rss .= "rn";
$rss .= "<![CDATA[{$this->channel_title}]]>rn";
$ rss .= "channel_description}]]>rn";
$rss .= "{$this-> channel_link}rn";
$rss .= "<言語>{$this->言語}rn";

if (!empty($this->pubDate) )
$rss .= "{$this->pubDate}rn";
if (!empty($this->lastBuildDate))
$rss .= "lastBuildDate}rn";
if (!empty($this->generator))
$rss .= "{$this->generator}< ;/generator>rn";

$rss .= "5rn";

if (!empty($this->channel_imgurl)) {
$rss .= "< image>rn";
$rss .= "<![CDATA[{$this->channel_title}]]>rn";
$rss .= "channel_link}rn";
$rss .= "{$this->channel_imgurl}rn";
$rss .= "< ;/image>rn";
}

for ($i = 0; $i items); $i++) {
$rss .= "rn";
$rss .= "<![CDATA[{$this->items[$i]['title']} ]]>rn";
$rss .= "{$this->items[$i]['link']}rn";
$rss .= "items[$i]['description']}]]>rn";
$rss .= "< pubDate>{$this->items[$i]['pubDate']}rn";
$rss .= "
rn";
}

$rss .= "
rn
";
return $rss;
}

/**
+------------------------------------------------ ----------
* RSS XML をブラウザに出力します
* +---------------------------- --- --------------------------------
* @access public
+-------- ------ -------------------------------------------- ------
* @return void
+------------------------------------- ------------ --------
*/
public function Display()
{
header("Content-Type: text/xml; charset =utf-8");
echo $this->Fetch();
exit;
}
}
?>

www.bkjia.com本当http://www.bkjia.com/PHPjc/444698.html技術記事 RSS 購読機能は多くの Web サイトで見つかりますが、次のコードは自分で作成したものであり、その中では RSS.class.php が使用されています。独占的に使用するので、受け取ります...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。