How to fetch news from RSS feeds using PHP

Ezekiel Oladejo
3 min readAug 20, 2018

I recently worked on a project which required that I pull news from some websites (some are built WordPress, some are not) and display them on the website itself so that visitors of the website can have access to the news from those websites easily and with just a single click, they are off to the original website to get the full gist 😄.

Load an RSS feed

So, this article is basically about how I did that, and what helped me achieve that. I figure someone might need it.

While I built my project on Laravel and the code snippets I will share are from the app, note that this same method can work if you’re working with Vanilla PHP — after all, Laravel is PHP 😉.

Let’s get to business! 💻

First thing to take note is when you want to fetch RSS items from a website is the correct URL to the feed. An example is ThreatPost RSS (which I will be using as reference in this article). ThreatPost is built on WordPress and so you can conclude (somehow) that WordPress websites have their RSS feeds on the /feed route of the website base URL. If the website you want to fetch from isn’t built on WordPress, you need to get the correct feed URL, some use aggregators like FeedProxy.

Now that we have the correct URL to our feed (https://threatpost.com/feed), we can now start fetching our news items…almost 😁

I used a library I found while researching a really simple way to execute this. The library serve as a wrapper for PHP’s SimpleXML.

To install the library, simply pull it from composer by running this in your command line: php composer.phar require dg/rss-php . After the package is pulled in to your project, you’re good to go. Easy steps is all you have left. You can practice this by simply using the link to the RSS feed right there in your code on the page you want it displayed. So, let’s see how I went about mine…

Load and display items from an RSS feed

With this snippet, you will display a card with the title and description of the news items and when the title is clicked, you will be redirected to the original content page. Really easy, right?

It’s as simple as that. If you want to display more stuff, you can go through the XML which is displayed when you go the feed URL, look in the <item> tags you find there and you will see the properties you can display — including image from the post (if any).

So, if you’re working with Vanilla PHP, it’s just a matter of stripping of the Blade expressions and write the Vanilla way 😎

I hope this helps.

Do you have queries, additions or a different approach, I’ll be glad to know! You can leave a comment right here or follow me on Twitter and send me a DM — I will respond as fast as possible 🍻

Keep hacking! 💻 😎 🍷

--

--

Ezekiel Oladejo

Software engineer building products that make life easier