grunjol/rss-php

RSS & Atom Feeds for PHP 是一个非常小巧且易于使用的库,用于消费 RSS 和 Atom 文件

1.2.9 2017-03-06 21:31 UTC

This package is not auto-updated.

Last update: 2024-09-18 20:23:07 UTC


README

Downloads this Month Latest Stable Version License

RSS & Atom Feeds for PHP 是一个非常小巧且易于使用的库,用于消费 RSS 和 Atom 文件。此项目是基于 David Grudl 的 rs-php https://github.com/dg/rss-php

它需要 PHP 5.5 和 Guzzle 6.1,并使用新 BSD 许可证。您可以从我们的 GitHub 仓库 获取最新版本,或者通过 Composer 安装

php composer.phar require grunjol/rss-php

用法

从 URL 下载 RSS 文件

$rss = Feed::loadRss($url);

返回的属性是 SimpleXMLElement 对象。从频道中提取信息很容易

echo 'Title: ', $rss->title;
echo 'Description: ', $rss->description;
echo 'Link: ', $rss->link;

foreach ($rss->item as $item) {
	echo 'Title: ', $item->title;
	echo 'Link: ', $item->link;
	echo 'Timestamp: ', $item->timestamp;
	echo 'Description ', $item->description;
	echo 'HTML encoded content: ', $item->{'content:encoded'};
}

从 URL 下载 Atom 文件

$atom = Feed::loadAtom($url);

您可以将自己的 Guzzle 实例设置到静态客户端属性

Feed::$client = new GuzzleHttp\Client(['headers' => ['User-Agent' => 'FeedPHP/1.0']]);

并且它将被重复使用。

您可以在每次调用中传递 Guzzle 请求选项(包括身份验证用户/密码)

$atom = Feed::loadAtom($url, ['auth' => ['peter', 'secret']);

您还可以通过 https://github.com/Kevinrob/guzzle-cache-middleware 启用缓存

//  Simple volatile memory cache example check docs for more options 
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;

// Create default HandlerStack
$stack = HandlerStack::create();

// Add this middleware to the top with `push`
$stack->push(new CacheMiddleware(), 'cache');

// Initialize the client with the handler option
Feed::$client = new Client(['handler' => $stack]);

版权所有 David Grudl, 2008 (http://davidgrudl.com) 版权所有 grunjol, 2017 (https://github.com/grunjol)