Use RSS feeds with Laravel

{title}

Applications do not only feed on their own data, although it is normal for us to have a database with private and more sensitive information, not always what we show comes from that database.
We have seen cases like we can feed our application from external sources such as CSV files, or text files, this is a way to obtain information from other sources, however it still requires some manual interaction.
Another way in which we can obtain external information is through RSS, these feeds can help us obtain updated data from other pages without having to consult our databases and the most interesting thing is that everything is online.

Use an RSS feed


In order to build this functionality we will need a standard installation of Laravel and then we will follow the following steps:
1 - We will create a new route in routes.php that will allow us to read the RSS feed, inside we must establish a variable with the URL to which we should consult; Then we get the content of this URL and place it inside a simplexml object to manipulate it at will in the application.
2 - Once we have simplexml, we must assign what we have captured to a new variable, in this way we can do an iteration for each element that we have captured from our RSS feed .
3 - At the time of doing this iteration we can access each of the elements that the RSS feed brings us, with it and the conversion that helped us to make simplexml, we can treat each one as an object attribute, in this way the Manipulation is simpler.
4 - Finally we can build an HTML structure that we must return for later use either in a controller or in a view.
Since we know the process that we must follow, we will see a small code that shows us how we can implement all this:

{title}


We can see that we have used simplexml_load_string () this method helps us to take a structured string, such as an HTML list or an RSS list, to XML format, to obtain this content we help ourselves with file_get_contents () applied to the RSS feed URL .
RSS structure

Finally we must know the structure that will have the RSS to be able to obtain the different elements by their names and also to locate the levels of nesting that they have. Another aspect that we should not forget is to make basic validations, such as showing if the source is incorrect or what happens if it is empty, this way we can avoid having errors that can tarnish our application.
With this we finished this tutorial, we saw that being able to obtain data from an external source such as RSS is quite easy, in fact Laravel only uses the structure since the rest is to use basic PHP tools, the important thing is that we demonstrate the simple and the powerful that can be the application of Laravel in our web solutions.

  • 0