RSS订阅示例
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/29 0029
 * Time: 16:39
 */

namespace app\index\controller;


use app\common\controller\Rss;
use app\common\controller\RSSItem;
use think\Controller;

class Feed extends Controller
{
    //rss设置
    public function index()
    {

        $config = get_config('store');
        $feed = new Rss();
        $feed->title       = $config['site_name'];
        $feed->link        = request()->domain();
        $feed->description = $config['store_description'];

        $chapter_ids = model('BlockChapter')->column('chapter_id');

        $chapters = model('NovelChapter')->alias('a')
            ->join('novel_info b','a.novel_id = b.id')
            ->field('a.id,a.code,a.title,a.novel_id,a.create_time,a.shelf_time,b.title as novel_title,b.desc')
            ->where(['a.is_open'=>1,'b.is_shelf'=>1,'b.status'=>['>',1],'a.is_look'=>['neq',1],'a.id'=>['not in',$chapter_ids]])
            ->order('a.shelf_time desc,a.create_time desc')->limit(30)->select();
        foreach ($chapters as $chapter){
            $item = new RSSItem();
            $item->title = $chapter['novel_title'].' - '.$chapter['code'].':'.$chapter['title'];
            $item->link  = url('novel/read',['nid'=>$chapter['novel_id'],'cid'=>$chapter['id']],'html',true);
            $item->category = $chapter['novel_title'];
            $item->setPubDate($chapter['shelf_time']);
            $desc = $chapter['desc'];
            $item->description = "<![CDATA[ $desc ]]>";
            $feed->addItem($item);
        }

        $feed->serve();
    }
}

Rss.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27 0027
 * Time: 9:58
 */

namespace app\common\controller;


class Rss
{
    var $title;
    var $link;
    var $description;
    var $language = "en-us";
    var $pubDate;
    var $items;
    var $tags;

    public function __construct()
    {
        $this->items = array();
        $this->tags  = array();
    }

    public function addItem($item)
    {
        $this->items[] = $item;
    }

    public function setPubDate($when)
    {
        if(strtotime($when) == false)
            $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
        else
            $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
    }

    public function getPubDate()
    {
        if(empty($this->pubDate))
            return date("D, d M Y H:i:s ") . "GMT";
        else
            return $this->pubDate;
    }

    public function addTag($tag, $value)
    {
        $this->tags[$tag] = $value;
    }

    public function out()
    {
        $out  = $this->header();
        $out .= "<channel>\n";
        $out .= "<title>" . $this->title . "</title>\n";
        $out .= "<link>" . $this->link . "</link>\n";
        $out .= "<description>" . $this->description . "</description>\n";
        $out .= "<language>" . $this->language . "</language>\n";
        $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";

        foreach($this->tags as $key => $val) {
            $out .= "<$key>$val</$key>\n";
        }
        foreach($this->items as $item){
            $out .= $item->out();
        }

        $out .= "</channel>\n";

        $out .= $this->footer();

        $out = str_replace("&", "&amp;", $out);

        return $out;
    }

    public function serve($contentType = "application/xml")
    {
        header("Content-Type:$contentType");
        $xml = $this->out();
        echo $xml;
      exit();
    }

    public function header()
    {
        $out  = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
        $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
        return $out;
    }

    public function footer()
    {
        return '</rss>';
    }
}

RSSItem.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27 0027
 * Time: 10:01
 */

namespace app\common\controller;


class RSSItem
{
    var $title;
    var $link;
    var $description;
    var $pubDate;
    var $guid;
    var $tags;
    var $attachment;
    var $length;
    var $mimetype;

    public function __construct()
    {
        $this->tags = array();
    }

    public function setPubDate($when)
    {
        if(strtotime($when) == false)
            $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
        else
            $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
    }

    public function getPubDate()
    {
        if(empty($this->pubDate))
            return date("D, d M Y H:i:s ") . "GMT";
        else
            return $this->pubDate;
    }

    public function addTag($tag, $value)
    {
        $this->tags[$tag] = $value;
    }

    public function out()
    {
        $out = "<item>\n";
        $out .= "<title>" . $this->title . "</title>\n";
        $out .= "<link>" . $this->link . "</link>\n";
        $out .= "<description>" . $this->description . "</description>\n";
        $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";

        if($this->attachment != ""){
            $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />";
        }

        if(empty($this->guid)){
            $this->guid = $this->link;
        }
        $out .= "<guid>" . $this->guid . "</guid>\n";
        foreach($this->tags as $key => $val){
            $out .= "<$key>$val</$key>\n";
        }
        $out .= "</item>\n";
        return $out;
    }

    public function enclosure($url, $mimetype, $length)
    {
        $this->attachment = $url;
        $this->mimetype   = $mimetype;
        $this->length     = $length;
    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇