popphp / pop-feed
此包已被废弃且不再维护。未建议替代包。
Pop PHP 框架的 Pop Feed 组件
2.1.0p1
2017-03-02 15:17 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 4.6.*
README
生命终止
pop-feed
组件 v2.1.0 现已生命终止,将不再维护。
概览
pop-feed
是一个用于生成和解析网页源的一个组件,同时尝试规范源中包含的常见节点和项目。
pop-feed
是 Pop PHP 框架 的一个组件。
安装
使用 Composer 安装 pop-feed
。
composer require popphp/pop-feed
基本用法
生成源
use Pop\Feed\Writer; $headers = [ 'published' => date('Y-m-d H:i:s'), 'author' => 'Test Author' ]; $items = [ [ 'title' => 'Some Item #1', 'link' => 'http://www.popphp.org/', 'description' => 'This is the description of item #1', 'published' => date('Y-m-d H:i:s') ], [ 'title' => 'Some Item #2', 'link' => 'http://popcorn.popphp.org/', 'description' => 'This is the description of item #2', 'published' => date('Y-m-d H:i:s') ] ]; $feed = new Writer($headers, $items); $feed->render();
渲染为 RSS 源
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"> <channel> <published>Tue, 21 Jul 2015 18:09:08 -0500</published> <author>Test Author</author> <item> <title>Some Item #1</title> <link>http://www.popphp.org/</link> <description>This is the description of item #1</description> <published>Tue, 21 Jul 2015 18:09:08 -0500</published> </item> <item> <title>Some Item #2</title> <link>http://popcorn.popphp.org/</link> <description>This is the description of item #2</description> <published>Tue, 21 Jul 2015 18:09:08 -0500</published> </item> </channel> </rss>
或者,渲染为 Atom 源
$feed = new Writer($headers, $items); $feed->setAtom(); $feed->render();
<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"> <published>Tue, 21 Jul 2015 18:10:39 -0500</published> <author> <name>Test Author</name> </author> <entry> <title>Some Item #1</title> <link href="http://www.popphp.org/" /> <description>This is the description of item #1</description> <published>Tue, 21 Jul 2015 18:10:39 -0500</published> </entry> <entry> <title>Some Item #2</title> <link href="http://popcorn.popphp.org/" /> <description>This is the description of item #2</description> <published>Tue, 21 Jul 2015 18:10:39 -0500</published> </entry> </feed>
解析源
如果源是 RSS 源
use Pop\Feed\Reader; use Pop\Feed\Format\Rss; $feed = new Reader(new Rss('http://www.domain.com/rss')); foreach ($feed->items as $item) { print_r($item); }
如果源是 Atom 源
use Pop\Feed\Reader; use Pop\Feed\Format\Atom; $feed = new Reader(new Atom('http://www.domain.com/feed')); foreach ($feed->entries as $entry) { print_r($entry); }