php 完美的rss 生成类

作者:袖梨 2022-07-02

class rssGenerator_rss
{
    var $rss_version = '2.0';
    var $encoding = '';
    var $stylesheet = '';

    function cData($str)
    {
        return '';
    }

    function createFeed($channel)
    {
        $selfUrl = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on' ? 'http://' : 'https教程://');
        $selfUrl .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        $rss = '         if (!empty($this->encoding)) {
            $rss .= ' encoding="' . $this->encoding . '"';
        }
        $rss .= '?>' . " ";
        if (!empty($this->stylesheet)) {
            $rss .= $this->stylesheet . " ";
        }
        $rss .= '' . " ";
        $rss .= ''">http://www.w3.org/2005/Atom">' . " ";
        $rss .= '  ' . " ";
     $rss .= '    ' . " ";
        $rss .= '    ' . $channel->title . '' . " ";
        $rss .= '    ' . $channel->link . '' . " ";
        $rss .= '    ' . $channel->description . '' . " ";
        if (!empty($channel->language)) {
            $rss .= '    ' . $channel->language . '' . " ";
        }
        if (!empty($channel->copyright)) {
            $rss .= '    ' . $channel->copyright . '' . " ";
        }
        if (!empty($channel->managingEditor)) {
            $rss .= '    ' . $channel->managingEditor . '' . " ";
        }
        if (!empty($channel->webMaster)) {
            $rss .= '    ' . $channel->webMaster . '' . " ";
        }
        if (!empty($channel->pubDate)) {
            $rss .= '    ' . $channel->pubDate . '' . " ";
        }
        if (!empty($channel->lastBuildDate)) {
            $rss .= '    ' . $channel->lastBuildDate . '' . " ";
        }
        foreach ($channel->categories as $category) {
            $rss .= '                if (!empty($category['domain'])) {
                $rss .= ' domain="' . $category['domain'] . '"';
            }
            $rss .= '>' . $category['name'] . '' . " ";
        }
        if (!empty($channel->generator)) {
            $rss .= '    ' . $channel->generator . '' . " ";
        }
        if (!empty($channel->docs)) {
            $rss .= '    ' . $channel->docs . '' . " ";
        }
        if (!empty($channel->ttl)) {
            $rss .= '    ' . $channel->ttl . '' . " ";
        }
        if (sizeof($channel->skipHours)) {
            $rss .= '    ' . " ";
            foreach ($channel->skipHours as $hour) {
                $rss .= '      ' . $hour . '' . " ";
            }
            $rss .= '   
' . " ";
        }
        if (sizeof($channel->skipDays)) {
            $rss .= '    ' . " ";
            foreach ($channel->skipDays as $day) {
                $rss .= '      ' . $day . '' . " ";
            }
            $rss .= '   
' . " ";
        }
        if (!empty($channel->image)) {
            $image = $channel->image;
            $rss .= '    ' . " ";
            $rss .= '      ' . $image->url . '' . " ";
            $rss .= '      ' . $image->title . '' . " ";
            $rss .= '      ' . $image->link . '' . " ";
            if ($image->width) {
                $rss .= '      ' . $image->width . '' . " ";
            }
            if ($image->height) {
                $rss .= '      ' . $image->height . '' . " ";
            }
            if (!empty($image->description)) {
                $rss .= '      ' . $image->description . '' . " ";
            }
            $rss .= '    ' . " ";
        }
        if (!empty($channel->textInput)) {
            $textInput = $channel->textInput;
            $rss .= '    ' . " ";
            $rss .= '      ' . $textInput->title . '' . " ";
            $rss .= '      ' . $textInput->description . '' . " ";
            $rss .= '      ' . $textInput->name . '' . " ";
            $rss .= '      ' . $textInput->link . '' . " ";
            $rss .= '   
' . " ";
        }
        if (!empty($channel->cloud_domain) || !empty($channel->cloud_path) || !empty($channel->cloud_registerProcedure) || !empty($channel->cloud_protocol)) {
            $rss .= '                $rss .= 'port="' . $channel->cloud_port . '" path="' . $channel->cloud_path . '" ';
            $rss .= 'registerProcedure="' . $channel->cloud_registerProcedure . '" ';
            $rss .= 'protocol="' . $channel->cloud_protocol . '" />' . " ";
        }
        if (!empty($channel->extraXML)) {
            $rss .= $channel->extraXML . " ";
        }
        foreach ($channel->items as $item) {
            $rss .= '    ' . " ";
            if (!empty($item->title)) {
                $rss .= '      ' . $item->title . '' . " ";
            }
            if (!empty($item->description)) {
                $rss .= '      ' . $item->description . '' . " ";
            }
            if (!empty($item->link)) {
                $rss .= '      ' . $item->link . '' . " ";
            }
            if (!empty($item->pubDate)) {
                $rss .= '      ' . $item->pubDate . '' . " ";
            }
            if (!empty($item->author)) {
                $rss .= '      ' . $item->author . '' . " ";
            }
            if (!empty($item->comments)) {
                $rss .= '      ' . $item->comments . '' . " ";
            }
            if (!empty($item->guid)) {
                $rss .= '      ';
                $rss .= $item->guid . '
' . " ";
            }
            if (!empty($item->source)) {
                $rss .= '      ';
                $rss .= $item->source . '' . " ";
            }
            if (!empty($item->enclosure_url) || !empty($item->enclosure_type)) {
                $rss .= '                      $rss .= 'length="' . $item->enclosure_length . '" ';
                $rss .= 'type="' . $item->enclosure_type . '" />' . " ";
            }
            foreach ($item->categories as $category) {
                $rss .= '                      if (!empty($category['domain'])) {
                    $rss .= ' domain="' . $category['domain'] . '"';
                }
                $rss .= '>' . $category['name'] . '' . " ";
            }
            $rss .= '   
' . " ";
        }
        $rss .= ' 
' . " ";
        return $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 $;
    var $;
    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();

}

?>

实例

require_once 'rss_generator.inc.php';

$rss_channel = new rssGenerator_channel();
$rss_channel->atomLinkHref = '';
$rss_channel->title = 'My News';
$rss_channel->link = 'http://111cn.net教程/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 = '[email protected] (Alex Jefferson)';
$rss_channel->webMaster = '[email protected] (Vagharshak Tozalakyan)';

$item = new rssGenerator_item();
$item->title = 'New website launched';
$item->description = 'Today I finaly launch a new website.';
$item->link = 'http://111com.net';
$item->guid = 'http://111com.net';
$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://111com.net';
$item->guid = 'http://111com.net';
$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);

?>

相关文章

精彩推荐