V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
moell
V2EX  ›  PHP

moell/rss 好用的轮子,轻松实现网站 RSS 功能,遵循 RSS2.0 标准

  •  
  •   moell · 2016-10-07 22:29:20 +08:00 · 1927 次点击
    这是一个创建于 2772 天前的主题,其中的信息可能已经有所发展或是发生改变。

    moell/rss

    moell/rss 是遵循 RSS 2.0 标准的 PHP 包,Laravel 版本请查看moell/laravel-rss

    源码地址

    moell/rss
    moell/laravel-rss

    RSS 规范

    http://www.rssboard.org/rss-specification

    要求

    PHP >= 5.4.0

    安装

    composer require "moell/rss:1.*"
    

    提供接口

    //设置字符集
    public function setEncode($encode); //默认 UTF-8
    
    public function channel(array $channel);
    
    public function item(array $item);
    
    public function items(array $items);
    
    //构造 xml
    public function build();
    
    //快速构造
    public function fastBuild(array $channel, array $item);
    
    public function __toString();
    

    用法

    $rss = new \Moell\Rss\Rss();
    
    $channel = [
        'title' => 'title',
        'link'  => 'http://moell.cn',
        'description' => 'description',
        'category' => [
            'value' => 'html',
            'attr' => [
                'domain' => 'http://www.moell.cn'
            ]
        ]
    ];
    
    $rss->channel($channel);
    
    $items = [];
    for($i = 0; $i < 2; $i++) {
        $item = [
            'title' => "title".$i,
            'description' => 'description',
            'source' => [
                'value' => 'moell.cn',
                'attr' => [
                    'url' => 'http://www.moell.cn'
                ]
            ]
        ];
        $items[] = $item;
        $rss->item($item);
    }
    
    echo $rss;	//获取 xml
    
    //其他获取方式
    $rss->build()->asXML();
    
    $rss->fastBuild($channel, $items)->asXML();
    
    $rss->channel($channel)->items($items)->build()->asXML();
    

    生成结果

    <?xml version="1.0" encoding="UTF-8"?>
    <rss
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
        <title>title</title>
        <link>http://moell.cn</link>
        <description>description</description>
        <category domain="http://www.moell.cn">html</category>
        <item>
            <title>title0</title>
            <description>description</description>
            <source url="http://www.moell.cn">moell.cn</source>
        </item>
        <item>
            <title>title1</title>
            <description>description</description>
            <source url="http://www.moell.cn">moell.cn</source>
        </item>
    </rss>
    

    License

    MIT

    如果你喜欢本项目,欢迎 star 。GitHub
    转帖需说明文章出处: http://moell.cn/article/25

    第 1 条附言  ·  2016-11-28 13:40:55 +08:00

    纠正错误

    <?xml version="1.0" encoding="UTF-8"?>
    <rss
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
        <channel>
            <title>title</title>
            <link>http://moell.cn</link>
            <description>description</description>
            <category domain="http://www.moell.cn">html</category>
            <item>
                <title>title0</title>
                <description>description</description>
                <source url="http://www.moell.cn">moell.cn</source>
            </item>
            <item>
                <title>title1</title>
                <description>description</description>
                <source url="http://www.moell.cn">moell.cn</source>
            </item>
        </channel>
    </rss>
    
    jellybool
        1
    jellybool  
       2016-10-08 00:27:33 +08:00
    学习一下单元测试吧。
    moell
        2
    moell  
    OP
       2016-10-08 07:09:14 +08:00
    @jellybool 谢谢 jellybool 的建议。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2890 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:37 · PVG 21:37 · LAX 06:37 · JFK 09:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.