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.
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.