read

RSS feeds are everywhere. There are dozen of ways to process and show them, but SimplePie has definitely become the go-to way of easily managing feeds within your PHP application.

Instalation

SimplePie is available as a Packagist package, so installing it is as simple as running this command in your Terminal:

$ require: "simplepie/simplepie": "dev-master"

The SimplePie class is now available in your project and you can easily make use of it.

Usage

SimplePie is well documented and there are plenty of examples around. Caching is something I had some trouble setting up, but by using the storage_path() function that Laravel provides on the Helpers package:

<?php
$feed = new SimplePie();
$feed->set_feed_url("http://blog.gigatrail.com/rss/");
$feed->enable_cache(true);
$feed->set_cache_location(storage_path().'/cache');
$feed->set_cache_duration(60*60*12);
$feed->set_output_encoding('utf-8');
$feed->init();

The feed is now initialized and you can pass it along to your views. Blade makes it extremely easy to use the feed object:

@foreach($feed->get_items(0,3) as $item)
    {{ $item->get_title() }}
    {{ $item->get_description() }}
@endforeach

And that's it! It's really that simple to start using SimplePie on your Laravel webapp.

Blog Logo

Rui Gomes

Web Developer, Internet Marketing enthusiast and Student@FEUP. That pretty much sums it up.


Published

Image

Rui Gomes

Knowledge share with a great deal of random thoughts.

Back to Overview