vinelab / rss
优雅的 RSS 2.0 和 Atom 客户端。
v2.0.3
2022-03-07 15:50 UTC
Requires
- php: >=7.1
- ext-simplexml: *
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0
- vinelab/http: *
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 7.*.*|8.*|9.*
README
RSS 客户端
一个简单而激进的 RSS 客户端,支持 RSS 0.92、2.0 和 Atom 聚合源。
概要
获取聚合源
$rss->feed('https://stackoverflow.com/feeds/tag?tagnames=php&sort=newest');
解析聚合源
$feed->info(); $feed->articles();
安装
composer require vinelab/rss
Laravel 配置
编辑 app.php 并将 'Vinelab\Rss\RssServiceProvider',
添加到 'providers'
数组。
它将自动别名自身为 RSS,因此无需在 app.php 中别名它,除非您想自定义它。在这种情况下,编辑您的 'aliases' 在 app.php 中添加 'MyRSS' => 'Vinelab\Rss\Facades\RSS',
用法
获取 RSS 聚合源
require 'vendor/autoload.php'; use Vinelab\Rss\Rss; $rss = new Rss(); $feed = $rss->feed('https://stackoverflow.com/feeds/tag?tagnames=php&sort=newest'); // $feed is now an instance of Vinelab\Rss\Feed $info = $feed->info(); $count = $feed->articlesCount(); $articles = $feed->articles();
聚合源信息
$info = $feed->info(); echo json_encode($info);
{ "title": "Newest questions tagged php - Stack Overflow", "subtitle": "most recent 30 from stackoverflow.com", "updated": "2020-07-16T19:14:29Z", "id": "https://stackoverflow.com/feeds/tag?tagnames=php&sort=newest" }
聚合源文章
访问文章
$articles = $feed->articles();
这将为您提供一个 Vinelab\Rss\ArticlesCollection
实例,它是 Illuminate\Support\Collection 的扩展。这个集合的每个项目都是一个 Vinelab\Rss\Article
实例,您可以安全地访问条目中的任何属性。
文章
是一个对象,其属性可以动态访问,如 $article->title
。
聚合源条目中存在的任何字段都将作为只读属性可访问,使 Article
成为不可变对象。
您也可以调用 isset($article->someField)
来检查指定条目是否存在该字段。
$article = $articles->first(); echo $article->title; // ABBA piano seen raising money, money, money at auction echo $article->whatever; // null
或者遍历文章
foreach ($feed->articles() as $article) { $title = $article->title; }
您还可以使用以下方式访问文章的原始 XML 格式
$article->xml();
有问题吗?
在 issues 中寻求帮助。