desarrolla2/rss-client
Deprecated RSS/Atom feed fetcher (will not be updated; migrate to FastFeed). Simple, fast RSS2.0/Atom1.0 client: add multiple feed URLs under a group name and fetch entries, with optional caching via desarrolla2/cache adapters.
Installation: Add the package via Composer:
composer require desarrolla2/rss-client
Note: Use dev-master if required (check Packagist).
Basic Usage: Initialize the client and fetch feeds:
use Desarrolla2\RSSClient\RSSClient;
$client = new RSSClient();
$client->addFeeds(['https://example.com/rss'], 'example_feed');
$feeds = $client->fetch('example_feed');
First Use Case: Display a list of headlines from a single feed in a Laravel blade template:
// In a Laravel controller
public function showFeed()
{
$client = new RSSClient();
$client->addFeeds(['https://example.com/rss'], 'headlines');
$feeds = $client->fetch('headlines', 5); // Limit to 5 items
return view('feed', ['items' => $feeds]);
}
<!-- In resources/views/feed.blade.php -->
@foreach($items as $item)
<div>{{ $item->title }}</div>
<small>{{ $item->pubDate }}</small>
@endforeach
Grouped Feed Management:
Organize feeds by logical groups (e.g., news, tech, sports) for easier maintenance:
$client->addFeeds([
'https://news.example.com/rss',
'https://tech.example.com/rss'
], 'tech_news');
Caching Integration:
Use Laravel’s cache system by wrapping desarrolla2/cache:
use Desarrolla2\Cache\Cache;
use Illuminate\Support\Facades\Cache as LaravelCache;
$cacheAdapter = new Cache(new LaravelCacheStore());
$client->setCache($cacheAdapter);
Note: Implement LaravelCacheStore to bridge Laravel’s cache with desarrolla2/cache.
Custom Processors for Data Transformation: Extend feed data with custom logic (e.g., extract thumbnails or keywords):
class ThumbnailProcessor implements ProcessorInterface
{
public function execute(NodeInterface $node, \DOMElement $item)
{
$thumbnail = $item->getElementsByTagName('media:thumbnail')->item(0)?->getAttribute('url');
if ($thumbnail) {
$node->setExtended('thumbnail', $thumbnail);
}
}
}
Register the processor:
$client->pushProcessor(new ThumbnailProcessor());
Laravel Service Provider Integration: Bind the client to Laravel’s service container for dependency injection:
// In AppServiceProvider
public function register()
{
$this->app->singleton(RSSClient::class, function ($app) {
$client = new RSSClient();
$client->addFeeds(config('feeds.groups'), config('feeds.cache_group'));
return $client;
});
}
Configure feeds in config/feeds.php:
return [
'groups' => [
'news' => ['https://news.example.com/rss'],
'tech' => ['https://tech.example.com/rss']
],
'cache_group' => 'feeds'
];
Queue-Based Fetching (Advanced): Offload feed fetching to a queue job to avoid blocking requests:
// FetchFeedsJob.php
public function handle()
{
$client = app(RSSClient::class);
$feeds = $client->fetch('news');
// Process or store feeds (e.g., save to DB)
}
Dispatch the job:
FetchFeedsJob::dispatch();
Daily Feed Aggregation:
// app/Console/Commands/FetchFeedsCommand.php
public function handle()
{
$client = app(RSSClient::class);
$feeds = $client->fetch('news');
foreach ($feeds as $feed) {
Feed::updateOrCreate(['url' => $feed->link], [
'title' => $feed->title,
'published_at' => $feed->pubDate
]);
}
}
app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('feeds:fetch')->daily();
}
Real-Time Updates:
// After fetching feeds, broadcast new items
broadcast(new FeedUpdated($newItems))->toOthers();
Feed Validation and Fallbacks:
try {
$feeds = $client->fetch('news');
} catch (\Exception $e) {
Log::error("Feed fetch failed: " . $e->getMessage());
// Fallback to cached data or default feed
$feeds = Cache::get('fallback_feeds', []);
}
Laravel Cache Adapter: Create a bridge for Laravel’s cache:
class LaravelCacheStore implements CacheInterface
{
public function get($key)
{
return Cache::get($key);
}
public function set($key, $value, $ttl = null)
{
Cache::put($key, $value, $ttl);
}
// Implement other CacheInterface methods...
}
Database Storage: Use Eloquent to store parsed feeds:
// Feed.php model
protected $fillable = ['title', 'link', 'description', 'pub_date', 'source_url'];
Testing: Mock the RSS client in tests:
$mockClient = Mockery::mock(RSSClient::class);
$mockClient->shouldReceive('fetch')->andReturn([$mockFeed]);
$this->app->instance(RSSClient::class, $mockClient);
Monitoring: Log feed fetch times and errors:
$start = microtime(true);
$feeds = $client->fetch('news');
Log::info("Fetched news feed in " . (microtime(true) - $start) . " seconds");
Deprecation Warning:
README or UPGRADING.md.Cache Configuration:
fetch() will hit external APIs, slowing down your app.$client->setCache(new Cache(new LaravelCacheStore()));
Feed Format Limitations:
No Async Support:
FetchFeedsJob::dispatchSync(); // For immediate results
Custom Processor Quirks:
ProcessorInterface and handle NodeInterface and DOMElement.class BaseProcessor implements ProcessorInterface
{
protected $sanitizer;
public function __construct($sanitizer)
{
$this->sanitizer = $sanitizer;
}
protected function getNodeValueByTagName(\DOMElement $item, $tagName)
{
// Implement shared logic...
}
}
Memory Usage:
fetch('group', 50) or stream results.SSL/TLS Issues:
curl or openssl settings or use a custom HTTP client.try {
$feeds = $client->fetch('news');
} catch (\Exception $e) {
Log::
How can I help you explore Laravel packages today?