debril / feed-io
PHP library built to consume and serve JSONFeed / RSS / Atom feeds
Installs: 1 539 299
Dependents: 9
Suggesters: 0
Security: 0
Stars: 255
Watchers: 14
Forks: 54
Open Issues: 12
Requires
- php: >=8.1
- ext-dom: *
- ext-json: *
- ext-libxml: *
- nyholm/psr7: ^1.5
- php-http/discovery: ^1.14
- php-http/httplug: ^2.3
- psr/http-client-implementation: *
- psr/log: ~1.0|~2.0|~3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- monolog/monolog: 1.*|2.*
- php-http/mock-client: ^1.5
- phpstan/phpstan: ^0.12.81
- phpunit/phpunit: ~9.3.0
Suggests
- monolog/monolog: Allows to handle logs
- php-http/guzzle7-adapter: Handles HTTP requests and responses
- dev-main
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.3.2
- v5.3.1
- v5.3.0
- v5.2.1
- v5.2.0
- v5.1.0
- v5.0.10
- v5.0.9
- v5.0.8
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v5.0.0-beta4
- v5.0.0-beta3
- v5.0.0-beta2
- v5.0.0-beta1
- v4.9.15
- v4.9.14
- v4.9.13
- v4.9.12
- v4.9.11
- v4.9.10
- V4.9.9
- v4.9.8
- v4.9.7
- v4.9.6
- v4.9.5
- v4.9.4
- v4.9.3
- v4.9.2
- v4.9.1
- v4.9.0
- v4.8.3
- v4.8.2
- v4.8.1
- v4.8.0
- v4.7.16
- v4.7.15
- v4.7.14
- v4.7.13
- v4.7.12
- v4.7.11
- v4.7.10
- v4.7.9
- v4.7.8
- v4.7.7
- v4.7.6
- v4.7.5
- v4.7.4
- v4.7.3
- v4.7.2
- v4.7.1
- v4.7.0
- v4.6.2
- v4.6.1
- v4.6.0
- v4.5.7
- v4.5.6
- v4.5.5
- v4.5.4
- v4.5.3
- v4.5.2
- v4.5.1
- v4.5.0
- v4.4.3
- v4.4.2
- v4.4.1
- v4.4.0
- v4.3.11
- v4.3.10
- v4.3.9
- v4.3.8
- v4.3.7
- v4.3.6
- v4.3.5
- v4.3.4
- v4.3.3
- v4.3.2
- v4.3.1
- v4.3.0
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.0
- v4.0.0-rc3
- v4.0.0-rc2
- v4.0.0-rc1
- v4.0.0-beta2
- v4.0.0-beta1
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.5.4
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.12
- v2.4.11
- v2.4.10
- v2.4.9
- v2.4.8
- v2.4.7
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- V2.3.2
- v2.3.1
- V2.3.0
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.0
- v1.1.0
- v1.0.1
- dev-dependabot/composer/symfony/process-6.4.14
- dev-release/5.3.x
- dev-release/4.9.x
This package is auto-updated.
Last update: 2024-11-06 16:11:25 UTC
README
feed-io is a PHP library built to consume and serve news feeds. It features:
- JSONFeed / Atom / RSS read and write support
- Feeds auto-discovery through HTML headers
- a Command line interface to discover and read feeds
- PSR-7 Response generation with accurate cache headers
- HTTP Headers support when reading feeds in order to save network traffic
- Detection of the format (RSS / Atom) when reading feeds
- Enclosure support to handle external medias like audio content
- Feed logo support (RSS + Atom)
- PSR compliant logging
- Content filtering to fetch only the newest items
- DateTime detection and conversion
- A generic HTTP ClientInterface
- Integrates with every PSR-18 compatible HTTP client.
This library is highly extensible and is designed to adapt to many situations, so if you don't find a solution through the documentation feel free to ask in the discussions.
Installation
Use Composer to add feed-io into your project's requirements :
composer require debril/feed-io
Requirements
feed-io 4 requires PHP 7.1+, feed-io 5 requires PHP 8.0+. All versions relies on psr/log
and any PSR-18 compliant HTTP client. To continue using you may require php-http/guzzle7-adapter
. it suggests monolog
for logging. Monolog is not the only library suitable to handle feed-io's logs, you can use any PSR/Log compliant library instead.
Usage
CLI
Let's suppose you installed feed-io using Composer, you can use its command line client to read feeds from your terminal :
./vendor/bin/feedio read http://php.net/feed.atom
reading
feed-io is designed to read feeds across the internet and to publish your own. Its main class is FeedIo :
// create a simple FeedIo instance, e.g. with the Symfony HTTP Client $client = new \FeedIo\Adapter\Http\Client(new Symfony\Component\HttpClient\HttplugClient()); $feedIo = \FeedIo\FeedIo($client); // read a feed $result = $feedIo->read($url); // get title $feedTitle = $result->getFeed()->getTitle(); // iterate through items foreach( $result->getFeed() as $item ) { echo $item->getTitle(); }
If you need to get only the new items since the last time you've consumed the feed, use the result's getItemsSince()
method:
// read a feed and specify the `$modifiedSince` limit to fetch only items newer than this date $result = $feedIo->read($url, $feed, $modifiedSince); // iterate through new items foreach( $result->getItemsSince() as $item ) { echo $item->getTitle(); }
You can also mix several filters to exclude items according to your needs:
// read a feed $result = $feedIo->read($url, $feed, $modifiedSince); // remove items older than `$modifiedSince` $since = new FeedIo\Filter\Since($result->getModifiedSince()); // Your own filter $database = new Acme\Filter\Database(); $chain = new Chain(); $chain ->add($since) ->add($database); // iterate through new items foreach( $result->getFilteredItems($chain) as $item ) { echo $item->getTitle(); }
In order to save bandwidth, feed-io estimates the next time it will be relevant to read the feed and get new items from it.
$nextUpdate = $result->getNextUpdate(); echo "computed next update: {$nextUpdate->format(\DATE_ATOM)}"; // you may need to access the statistics $updateStats = $result->getUpdateStats(); echo "average interval in seconds: {$updateStats->getAverageInterval()}";
feed-io calculates the next update time by first detecting if the feed was active in the last 7 days and if not we consider it as sleepy. The next update date for a sleepy feed is set to the next day at the same time. If the feed isn't sleepy we use the average interval and the median interval by adding those intervals to the feed's last modified date and compare the result to the current time. If the result is in the future, then it's returned as the next update time. If none of them are in the future, we considered the feed will be updated quite soon, so the next update time is one hour later from the moment of the calculation.
Please note: the fixed delays for sleepy and closed to be updated feeds can be set through Result::getNextUpdate()
arguments, see Result for more details.
Feeds discovery
A web page can refer to one or more feeds in its headers, feed-io provides a way to discover them :
// create a simple FeedIo instance, e.g. with the Symfony HTTP Client $client = new \FeedIo\Adapter\Http\Client(new Symfony\Component\HttpClient\HttplugClient()); $feedIo = \FeedIo\FeedIo($client); $feeds = $feedIo->discover($url); foreach( $feeds as $feed ) { echo "discovered feed : {$feed}"; }
Or you can use feed-io's command line :
./vendor/bin/feedio discover https://a-website.org
You'll get all discovered feeds in the output.
formatting an object into a XML stream
// build the feed $feed = new FeedIo\Feed; $feed->setTitle('...'); // convert it into Atom $atomString = $feedIo->toAtom($feed); // or ... $atomString = $feedIo->format($feed, 'atom');
Adding a StyleSheet
$feed = new FeedIo\Feed; $feed->setTitle('...'); $styleSheet = new StyleSheet('http://url-of-the-xsl-stylesheet.xsl'); $feed->setStyleSheet($styleSheet);
building a feed including medias
// build the feed $feed = new FeedIo\Feed; $feed->setTitle('...'); $item = $feed->newItem(); // add namespaces $feed->setNS( 'itunes', //namespace 'http://www.itunes.com/dtds/podcast-1.0.dtd' //dtd for the namespace ); $feed->set('itunes,title', 'Sample Title'); //OR any other element defined in the namespace. $item->addElement('itunes:category', 'Education'); // build the media $media = new \FeedIo\Feed\Item\Media $media->setUrl('http://yourdomain.tld/medias/some-podcast.mp3'); $media->setType('audio/mpeg'); // add it to the item $item->addMedia($media); $feed->add($item);
Creating a valid PSR-7 Response with a feed
You can turn a \FeedIo\FeedInstance
directly into a PSR-7 valid response using \FeedIo\FeedIo::getPsrResponse()
:
$feed = new \FeedIo\Feed; // feed the beast ... $item = new \FeedIo\Feed\Item; $item->set ... $feed->add($item); $atomResponse = $feedIo->getPsrResponse($feed, 'atom'); $jsonResponse = $feedIo->getPsrResponse($feed, 'json');
Building a FeedIo instance
To create a new FeedIo instance you only need to inject two dependencies :
- an HTTP Client implementing FeedIo\Adapter\ClientInterface. It can be wrapper for an external library like FeedIo\Adapter\Guzzle\Client
- a PSR-3 logger implementing Psr\Log\LoggerInterface
// first dependency : the HTTP client // here we use Guzzle as a dependency for the client $guzzle = new GuzzleHttp\Client(); // Guzzle is wrapped in this adapter which is a FeedIo\Adapter\ClientInterface implementation $client = new FeedIo\Adapter\Guzzle\Client($guzzle); // second dependency : a PSR-3 logger $logger = new Psr\Log\NullLogger(); // now create FeedIo's instance $feedIo = new FeedIo\FeedIo($client, $logger);
Another example with Monolog configured to write on the standard output :
// create a simple FeedIo instance, e.g. with the Symfony HTTP Client $client = new \FeedIo\Adapter\Http\Client(new Symfony\Component\HttpClient\HttplugClient()); $logger = new Monolog\Logger('default', [new Monolog\Handler\StreamHandler('php://stdout')]); $feedIo = \FeedIo\FeedIo($client, $logger);
Inject a custom Logger
You can inject any Logger you want as long as it implements Psr\Log\LoggerInterface
. Monolog does, but it's not the only library : https://packagist.org/providers/psr/log-implementation
use FeedIo\FeedIo; use FeedIo\Adapter\Guzzle\Client; use GuzzleHttp\Client as GuzzleClient; use Custom\Logger; $client = new Client(new GuzzleClient()); $logger = new Logger(); $feedIo = new FeedIo($client, $logger);
Inject a custom HTTP Client
Since 6.0 there is a generic HTTP adapter that wraps any PST-18 compliant HTTP client.
use CustomPsr18\Client as CustomClient; $client = new Custom\Adapter\Http\Client(new CustomClient()) $logger = new Psr\Log\NullLogger(); $feedIo = new FeedIo\FeedIo($client, $logger);
Configure feed-io using the Factory
The factory has been deprecated in feed-io 5.2 and was removed in 6.0. Instantiate the facade directly and pass in the desired HTTP client and logger interface.
Dealing with missing timezones
Sometimes you have to consume feeds in which the timezone is missing from the dates. In some use-cases, you may need to specify the feed's timezone to get an accurate value, so feed-io offers a workaround for that :
$feedIo->getDateTimeBuilder()->setFeedTimezone(new \DateTimeZone($feedTimezone)); $result = $feedIo->read($feedUrl); $feedIo->getDateTimeBuilder()->resetFeedTimezone();
Don't forget to reset feedTimezone
after fetching the result, or you'll end up with all feeds located in the same timezone.
Built with PHP Storm
Most of feed-io's code was written using PHP Storm courtesy of Jetbrains.