Posted by Arjun Arjun on Wednesday 23rd March 2016, 1 minute read

Reading RSS Feed with PHP

PHP Code Snippets RSS Inspiration XML DOMDocument

This article shows how to read RSS feeds using php script. If you are reading this post you might know about RSS feeds and it's benefits. It is good to display your blog feeds as like recent articles list to keep people in the loop.

As you know RSS feed data is in XML format. fortunately PHP 5 introduced the DOM extension which make it easy to work with XML data. You can also use simplexml_load_file() function instead of DOM extension, which will interprets an XML file into an object.

Reading RSS Feed with PHP

The following code will create a new DOMDocument() object and into we will load the WordPress.org RSS feed and we will store the response in $feeds variable.

	$rss = new DOMDocument();
	$rss->load('http://wordpress.org/news/feed/');
	$feeds = array();
	foreach ($rss->getElementsByTagName('item') as $node) {
		$item = array ( 
			'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
			'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
			'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
			'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
			);
		array_push($feeds, $item);
	}

Now you can iterate over the $feeds data and display the results as you wish. That is it.

...
Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.