Skip to content
2 min readLaravel, My Projects

Laravel Feed Reader

A simple RSS feed reader for Laravel, built on top of SimplePie. One call reads any RSS or Atom feed, with caching and configurable profiles.

Github: https://github.com/vedmant/laravel-feed-reader

Reading a feed takes a single line:

$f = FeedReader::read('https://news.google.com/news/rss');

echo $f->get_title();
echo $f->get_items()[0]->get_title();
echo $f->get_items()[0]->get_content();

Features

  • One command to read any RSS or Atom feed
  • Multiple feed profiles, each with its own settings
  • Built-in caching through any Laravel cache driver
  • Per-call cURL options, e.g. for feeds behind authentication

Installation

Via Composer

$ composer require vedmant/laravel-feed-reader

This package supports Laravel Auto-Discovery and will be discovered automatically.

For Laravel versions before 5.5 add the service provider to the providers array in config/app.php, and optionally the facade alias:

'providers' => [
    // ...
    Vedmant\FeedReader\FeedReaderServiceProvider::class,
],

'aliases' => [
    // ...
    'FeedReader' => Vedmant\FeedReader\Facades\FeedReader::class,
],

There is also a video tutorial that walks through the setup.

Configuration

Publish the configuration file:

php artisan vendor:publish --provider="Vedmant\FeedReader\FeedReaderServiceProvider"

The published config/feed-reader.php contains an array of profiles that define how feeds are read. The default profile is used unless you pass another one:

'profiles' => [
    'default' => [
        'cache' => [
            'duration' => 3600,                  // seconds
            'enabled' => true,
            'driver' => env('CACHE_DRIVER', 'file'),
        ],
        'force-feed' => false,                   // treat the data as a feed even if the headers say otherwise
        'order-by-date' => false,
        'ssl-verify' => true,                    // set to false for self-signed certificates
    ],
],

You can add as many profiles as you need, for example one with caching disabled for feeds that must always be fresh.

Usage

Call the read method with a feed URL. It accepts the profile name as an optional second argument:

$f = FeedReader::read('https://news.google.com/news/rss', 'default');

It returns a SimplePie object with the feed loaded, so everything in the SimplePie API is available, for example:

foreach ($f->get_items() as $item) {
    echo $item->get_title();
    echo $item->get_permalink();
    echo $item->get_date('Y-m-d');
}

Passing cURL options

You can also pass cURL options for a single read() call as an array in the third parameter. The full list of options is in the PHP manual.

For example, to read a feed protected with Digest authentication:

$options = [
    'curl_options' => [
        CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
        CURLOPT_USERPWD => 'username:password',
    ],
];

$f = FeedReader::read('https://news.google.com/news/rss', 'default', $options);

License

This project is free to use for any purpose and licensed under the MIT License. Any issues, please report them on GitHub.

Comments

No comments yet.

Leave a comment