Using RSS as a data source
Many blogs and news sites offer RSS feeds of their content. Using Laravel, we can get those feeds and display them as a feed reader, or even save them in our own database.
Getting ready
For this recipe, we just need a standard Laravel installation, and RSS URL to use.
How to do it...
To complete this recipe, follow this step:
Create a new route in our
routes.php
file to read in the RSS:Route::get('rss', function() { $source = 'http://rss.cnn.com/rss/cnn_topstories.rss'; $headers = get_headers($source); $response = substr($headers[0], 9, 3); if ($response == '404') { return 'Invalid Source'; } $data = simplexml_load_string(file_get_contents($source)); if (count($data) == 0) { return 'No Posts'; } $posts = ''; foreach($data->channel->item as $item) { $posts .= '<h1><a href="' . $item->link . '">'. $item->title . '</a></h1>'; ...