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

2016-10-07 22:29:20 +08:00
 moell

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

1933 次点击
所在节点    PHP
2 条回复
jellybool
2016-10-08 00:27:33 +08:00
学习一下单元测试吧。
moell
2016-10-08 07:09:14 +08:00
@jellybool 谢谢 jellybool 的建议。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/311027

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX