Cart
Your cart is currently empty.

How to Parse RSS Feeds with PHP: An Example Using BBC News

APIs (application programming interfaces) have had arguably one of the biggest influences on the web over the past 5 years. The ability to leverage a company’s data and services, quite often for free (although it might be rate limited), to better serve your own users is an absolute blessing to developers. Just think about it for a second. Businesses that rely on their API usage would either not even be around today or wouldn’t have been able to grow anywhere near as fast without APIs being a ‘th
How to Parse RSS Feeds with PHP: An Example Using BBC News

APIs (application programming interfaces) have had arguably one of the biggest influences on the web over the past 5 years.

The ability to leverage a company’s data and services, quite often for free (although it might be rate limited), to better serve your own users is an absolute blessing to developers.


Who uses APIs?

There are a number of different industries that rely heavily on API usage one way or another:

  • Social & blogging: Facebook, LinkedIn, WordPress, Flickr and Reddit

  • Email service providers: MailChimp, SendGrid, Mailgun, Vertical Response, Constant Contact

  • Payment services: Stripe, Balanced Payments, GoCardless, Paymill, Coinbase

  • Cloud storage: Google Drive, Dropbox, GitHub, Box

  • Miscellaneous: Spotify, IMDB, Yoda Speak, Foursquare


Let’s not forget that at a very low level, RSS feeds are to a certain extent APIs also. Thousands of businesses rely on parsing RSS feeds to serve content to users.

Parsing the BBC News API using PHP

You'll be delighted to know that this is actually ridiculously simple; it only really requires a couple of lines of PHP! You'll need a local webserver (Wamp/Mamp/Xamp), an RSS feed URL (http://feeds.bbci.co.uk/news/rss.xml), and your brain.

Loading up the RSS content

Since we know the RSS feed is in XML format, PHP5 has a handy built-in function called SimpleXMLElement whose job is to parse XML formatted content:

$newsoutput = new SimpleXMLElement('http://feeds.bbci.co.uk/news/rss.xml', true);

My preference is to turn this from the simpleXmlElement object into a normal array:

$newsoutput = new SimpleXMLElement('http://feeds.bbci.co.uk/news/rss.xml', LIBXML_NOCDATA, true); $newsoutput = json_decode(json_encode($newsoutput), TRUE);

Now we're ready to parse this and loop through the posts one-by-one. I've added a form that POSTs back to itself with a hidden input called "submitted", so we can check whether the button has been pressed before parsing.

Outputting the feed news items

Each individual news item sits inside channel > item, with fields like title, description, link, guid, and pubDate. Here's how we loop through these:

foreach ($newsoutput['channel']['item'] as $item) { echo $item['title']; }

With some simple Bootstrap styling applied, the finalised BBC News RSS reader comes together nicely.

So there we have it! Parsing an RSS feed with PHP is extremely simple. Now you've got the data on your server, you could always create a mashup with a different API — translate headlines into Yoda Speak, or create a word cloud for each article. With APIs becoming so prevalent on the web, only your creativity can hold you back!

Related reading:


Image with text

Pair large text with an image to tell a story, explain a detail about your product, or describe a new promotion.