aschmelyun / basic-feeds
一个简单的PHP库,用于生成RSS和Atom订阅源
v1.0.0
2022-04-02 05:39 UTC
Requires
- php: ^7.4|^8
Requires (Dev)
- phpunit/phpunit: ^9.5
README
本包提供了一种使用简化语法在PHP中生成RSS 2和Atom XML订阅源的基本方法。本包没有外部依赖,因为它依赖于内置的SimpleXMLElement类。
安装
composer require aschmelyun/basic-feeds
使用
通过使用Feed::create()
方法并传入设置feed基础所需的属性数组来初始化feed。
use Aschmelyun\BasicFeeds\Feed; $feed = Feed::create([ 'link' => 'https://example.com/blog', 'authors' => 'Andrew Schmelyun', 'title' => 'My Example Blog', 'feed' => 'https://example.com/feed.xml', ]);
然后,在Feed对象上使用entry()
方法将条目附加到feed中。
$feed->entry([ 'title' => 'Post One', 'link' => 'https://example.com/blog/post-one', 'summary' => 'This is my summary', 'content' => '<p>This is my example content!</p>' ]);
entry()
方法也可以一次性接收多个项目,使用嵌套数组。
$feed->entry([ 'title' => 'Post Two', 'link' => 'https://example.com/blog/post-two', 'summary' => 'This is my second summary', 'content' => '<p>This is my second example content!</p>' ], [ 'title' => 'Post Three', 'link' => 'https://example.com/blog/post-three', 'summary' => 'This is my third summary', 'content' => '<p>This is my third example content!</p>' ]);
然后,您可以使用asAtom()
或asRss()
方法将feed编译成所需格式,并以XML字符串形式返回。
$xml = $feed->asAtom(); // $xml = $feed->asRss();
要求
有一些必需的属性,并且它们取决于您选择的格式。
对于Feed::create()
,它们是
- RSS:
title
、link
、description
- Atom:
title
、link
、authors
、feed
对于entry()
,它们是
- RSS:
title
、link
、description
- Atom:
title
、link
、summary
、content
扩展
本包包含一个接口,您可以使用它来扩展本包以创建自己的feed生成器。
测试
如果您当前的项目中还没有安装PHPUnit,请使用dev依赖项安装此包
composer require aschmelyun/basic-feeds --dev
然后运行PHPUnit测试
./vendor/bin/phpunit
许可证
本包采用MIT许可证。有关详细信息,请参阅LICENSE。