Grab & parse RSS feeds using PHP / Laravel

Do you ever wanted to grab & parse rss feed using php ? if yes , then you may be familiar with SimplePie.

SimplePie is an amazing PHP package that help you parse any rss feed , its really incredible and very flexible when it comes to advanced usages.
In this article i will show you how to make a simple rss parser using php & laravel framework
let's jump in

first lets install fresh laravel application



composer create-project --prefer-dist laravel/laravel rss

next add the

"willvincent/feeds": "1.1.*"

to your composer.json and run composer update to install willvincent/feed package which is laravel package work as a wrapper to simplepie for laravel.

let's add a new route to our controller GrabController.php

Route::get('/grab','GrabController@start');

then in the controller let's do the magic

$feed = \Feeds::make('http://url/path/to/rss');
echo $feed->get_title(); //this will get you the title of the rss
echo '<hr />';
echo $feed->get_permalink(); //this will get you the link of the rss
echo '<hr />';
$items = $feed->get_items(); //grab all items inside the rss
foreach($items as $item):
echo $item->get_title(); //get the title of single news
echo '<br />';
echo $item->get_permalink(); //get the link of single news
echo '<br />';
$enclosure = $item->get_enclosures();
//retrive the enclosures (extras ex: attached media)
foreach($enclosure as $enc){
//print_r($enc);
}
echo $item->get_description(); //get the link of single news
echo '<hr />';
endforeach;

so the above code is the most usage methods when you want to parse and rss , if you want futher info see : https://github.com/willvincent/feeds & http://simplepie.org/wiki/

Comments

Post a Comment

Popular Posts