php 完美的rss 生成类
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-20 15:49:28 浏览: 评论:0
- <?php
- class rssGenerator_rss
- {
- var $rss_version = '2.0';
- var $encoding = '';
- var $stylesheet = '';
- function cData($str)
- {
- return '<![CDATA[ ' . $str . ' ]]>';
- }
- function createFeed($channel)
- {
- $selfUrl = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on' ? 'http://' : 'https教程://');
- $selfUrl .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
- $rss = '<?xml version="1.0"';
- if (!emptyempty($this->encoding)) {
- $rss .= ' encoding="' . $this->encoding . '"';
- }
- $rss .= '?>' . " ";
- if (!emptyempty($this->stylesheet)) {
- $rss .= $this->stylesheet . " ";
- }
- $rss .= '<!-- Generated on ' . date('r') . ' -->' . " ";
- $rss .= '<rss version="' . $this->rss_version . '" xmlns:atom="http://www.w3.org/2005/Atom">' . " ";
- $rss .= ' <channel>' . " ";
- $rss .= ' <atom:link href="' . ($channel->atomLinkHref ? $channel->atomLinkHref : $selfUrl) . '" rel="self" type="application/rss+xml" />' . " ";
- $rss .= ' <title>' . $channel->title . '</title>' . " ";
- $rss .= ' <link>' . $channel->link . '</link>' . " ";
- $rss .= ' <description>' . $channel->description . '</description>' . " ";
- if (!emptyempty($channel->language)) {
- $rss .= ' <language>' . $channel->language . '</language>' . " ";
- }
- if (!emptyempty($channel->copyright)) {
- $rss .= ' <copyright>' . $channel->copyright . '</copyright>' . " ";
- }
- if (!emptyempty($channel->managingEditor)) {
- $rss .= ' <managingEditor>' . $channel->managingEditor . '</managingEditor>' . " ";
- }
- if (!emptyempty($channel->webMaster)) {
- $rss .= ' <webMaster>' . $channel->webMaster . '</webMaster>' . " ";
- }
- if (!emptyempty($channel->pubDate)) {
- $rss .= ' <pubDate>' . $channel->pubDate . '</pubDate>' . " ";
- }
- if (!emptyempty($channel->lastBuildDate)) {
- $rss .= ' <lastBuildDate>' . $channel->lastBuildDate . '</lastBuildDate>' . " ";
- }
- foreach ($channel->categories as $category) {
- $rss .= ' <category';
- if (!emptyempty($category['domain'])) {
- $rss .= ' domain="' . $category['domain'] . '"';
- }
- $rss .= '>' . $category['name'] . '</category>' . " ";
- }
- if (!emptyempty($channel->generator)) {
- $rss .= ' <generator>' . $channel->generator . '</generator>' . " ";
- }
- if (!emptyempty($channel->docs)) {
- $rss .= ' <docs>' . $channel->docs . '</docs>' . " ";
- }
- if (!emptyempty($channel->ttl)) {
- $rss .= ' <ttl>' . $channel->ttl . '</ttl>' . " ";
- }
- if (sizeof($channel->skipHours)) {
- $rss .= ' <skipHours>' . " ";
- foreach ($channel->skipHours as $hour) {
- $rss .= ' <hour>' . $hour . '</hour>' . " ";
- }
- $rss .= ' </skipHours>' . " ";
- }
- if (sizeof($channel->skipDays)) {
- $rss .= ' <skipDays>' . " ";
- foreach ($channel->skipDays as $day) {
- $rss .= ' <day>' . $day . '</day>' . " ";
- }
- $rss .= ' </skipDays>' . " ";
- }
- if (!emptyempty($channel->image)) {
- $image = $channel->image;
- $rss .= ' <image>' . " ";
- $rss .= ' <url>' . $image->url . '</url>' . " ";
- $rss .= ' <title>' . $image->title . '</title>' . " ";
- $rss .= ' <link>' . $image->link . '</link>' . " ";
- if ($image->width) {
- $rss .= ' <width>' . $image->width . '</width>' . " ";
- }
- if ($image->height) {
- $rss .= ' <height>' . $image->height . '</height>' . " ";
- }
- if (!emptyempty($image->description)) {
- $rss .= ' <description>' . $image->description . '</description>' . " ";
- }
- $rss .= ' </image>' . " ";
- }
- if (!emptyempty($channel->textInput)) {
- $textInput = $channel->textInput;
- $rss .= ' <textInput>' . " ";
- $rss .= ' <title>' . $textInput->title . '</title>' . " ";
- $rss .= ' <description>' . $textInput->description . '</description>' . " ";
- $rss .= ' <name>' . $textInput->name . '</name>' . " ";
- $rss .= ' <link>' . $textInput->link . '</link>' . " ";
- $rss .= ' </textInput>' . " ";
- }
- if (!emptyempty($channel->cloud_domain) || !emptyempty($channel->cloud_path) || !emptyempty($channel->cloud_registerProcedure) || !emptyempty($channel->cloud_protocol)) {
- $rss .= ' <cloud domain="' . $channel->cloud_domain . '" ';
- $rss .= 'port="' . $channel->cloud_port . '" path="' . $channel->cloud_path . '" ';
- $rss .= 'registerProcedure="' . $channel->cloud_registerProcedure . '" ';
- $rss .= 'protocol="' . $channel->cloud_protocol . '" />' . " ";
- }
- if (!emptyempty($channel->extraXML)) {
- $rss .= $channel->extraXML . " ";
- }
- foreach ($channel->items as $item) {
- $rss .= ' <item>' . " ";
- if (!emptyempty($item->title)) {
- $rss .= ' <title>' . $item->title . '</title>' . " ";
- }
- if (!emptyempty($item->description)) {
- $rss .= ' <description>' . $item->description . '</description>' . " ";
- }
- if (!emptyempty($item->link)) {
- $rss .= ' <link>' . $item->link . '</link>' . " ";
- }
- if (!emptyempty($item->pubDate)) {
- $rss .= ' <pubDate>' . $item->pubDate . '</pubDate>' . " ";
- }
- if (!emptyempty($item->author)) {
- $rss .= ' <author>' . $item->author . '</author>' . " ";
- }
- if (!emptyempty($item->comments)) {
- $rss .= ' <comments>' . $item->comments . '</comments>' . " ";
- }
- if (!emptyempty($item->guid)) {
- $rss .= ' <guid isPermaLink="';
- $rss .= ($item->guid_isPermaLink ? 'true' : 'false') . '">';
- $rss .= $item->guid . '</guid>' . " ";
- }
- if (!emptyempty($item->source)) {
- $rss .= ' <source url="' . $item->source_url . '">';
- $rss .= $item->source . '</source>' . " ";
- }
- if (!emptyempty($item->enclosure_url) || !emptyempty($item->enclosure_type)) {
- $rss .= ' <enclosure url="' . $item->enclosure_url . '" ';
- $rss .= 'length="' . $item->enclosure_length . '" ';
- $rss .= 'type="' . $item->enclosure_type . '" />' . " ";
- }
- foreach ($item->categories as $category) {
- $rss .= ' <category';
- if (!emptyempty($category['domain'])) {
- $rss .= ' domain="' . $category['domain'] . '"';
- }
- $rss .= '>' . $category['name'] . '</category>' . " ";
- }
- $rss .= ' </item>' . " ";
- }
- $rss .= ' </channel>' . " ";
- return $rss .= '</rss>';
- }
- }
- class rssGenerator_channel
- {
- var $atomLinkHref = '';
- var $title = '';
- var $link = '';
- var $description = '';
- var $language = '';
- var $copyright = '';
- var $managingEditor = '';
- var $webMaster = '';
- var $pubDate = '';
- var $lastBuildDate = '';
- var $categories = array();
- var $generator = '';
- var $docs = '';
- var $ttl = '';
- var $image = '';
- var $textInput = '';
- var $skipHours = array();
- var $skipDays = array();
- var $cloud_domain = '';
- var $cloud_port = '80';
- var $cloud_path = '';
- var $cloud_registerProcedure = '';
- var $cloud_protocol = '';
- var $items = array();
- var $extraXML = '';
- }
- class rssGenerator_image
- {
- var $url = '';
- var $title = '';
- var $link = '';
- var $width = '88';
- var $height = '31';
- var $description = '';
- }
- class rssGenerator_textInput
- {
- var $title = '';
- var $description = '';
- var $name = '';
- var $link = '';
- }
- class rssGenerator_item
- {
- var $title = '';
- var $description = '';
- var $link = '';
- var $author = '';
- var $pubDate = '';
- var $comments = '';
- var $guid = '';
- var $guid_isPermaLink = true;
- var $source = '';
- var $source_url = '';
- var $enclosure_url = '';
- var $enclosure_length = '0';
- var $enclosure_type = '';
- var $categories = array();
- }
- //开源代码phpfensi.com
- ?>
php使用实例如下:
- <?php
- require_once 'rss_generator.inc.php';
- $rss_channel = new rssGenerator_channel();
- $rss_channel->atomLinkHref = '';
- $rss_channel->title = 'My News';
- $rss_channel->link = 'http://phpfensi.com/news.php';
- $rss_channel->description = 'The latest news about web-development.';
- $rss_channel->language = 'en-us';
- $rss_channel->generator = 'PHP RSS Feed Generator';
- $rss_channel->managingEditor = 'editor@mysite.com (Alex Jefferson)';
- $rss_channel->webMaster = 'webmaster@mysite.com (Vagharshak Tozalakyan)';
- $item = new rssGenerator_item();
- $item->title = 'New website launched';
- $item->description = 'Today I finaly launch a new website.';
- $item->link = 'http://phpfensi.com';
- $item->guid = 'http://phpfensi.com';
- $item->pubDate = 'Tue, 07 Mar 2006 00:00:01 GMT';
- $rss_channel->items[] = $item;
- $item = new rssGenerator_item();
- $item->title = 'Another website launched';
- $item->description = 'Just another website launched.';
- $item->link = 'http://phpfensi.com';
- $item->guid = 'http://phpfensi.com';
- $item->pubDate = 'Wed, 08 Mar 2006 00:00:01 GMT';
- $rss_channel->items[] = $item;
- $rss_feed = new rssGenerator_rss();
- $rss_feed->encoding = 'UTF-8';
- $rss_feed->version = '2.0';
- header('Content-Type: text/xml');
- echo $rss_feed->createFeed($rss_channel);
- ?>
Tags: php rss 生成类
- 上一篇:PHP RSS 生成类
- 下一篇:php 3des加密解密类程序代码
相关文章
- ·PHP多功能图片处理类(2013-11-11)
- ·PHP 生成缩略图的类(2013-11-13)
- ·一个分页显示类(2013-11-13)
- ·分享的一个分页类(2013-11-13)
- ·简单的php分页类(2013-11-14)
- ·一个功能比较高的分页类(for PHP5.x)(2013-11-28)
- ·phpword中文字符乱码解决办法(2013-12-05)
- ·一个比较完善的购物车类(2013-12-08)
- ·php面象对象数据库操作类(2013-12-09)
- ·PHP顶层类(2013-12-10)
- ·PHP静态文件生成类(2013-12-10)
- ·PHP分页类(2013-12-10)
- ·非常简单的日历类(2013-12-11)
- ·php数字分页类的代码(2013-12-23)
- ·PHP货币换算程序代码(2013-12-27)
- ·实现多文件上传php类(2014-01-03)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)