So I just set up this blog and decided I wanted my new web page to display the feed. Though I found some nice widgets available, I really wanted to be able to customize my feed. After searching for a little, I found a great post on SoftArea51.com that explained how to parse RSS feeds with PHP. This post was exactly what I needed. They went into full detain about how the process worked and made a great little script that took each post and put it into an array. All I needed to do was ad a small script to output those posts the way I wanted.

Now, there was a catch… The script worked great locally on my machine, however when I put it on my website, it had trouble. I was getting an error message that read something like “Warning: DOMDocument::load(...". After searching online, I found another great post right on the PHP.net website that had the solution for loading documents over HTTP. I simply took that code and added it to the top of my script. It fixed the problem right away. No extra coding!

I saved my code as a separate file named rss.php. Then, I simply included it on my home page. Now my home page will contain fresh content each time I create a new post, which is a huge bonus from an SEO perspective.

I’ve provided the code for this below. All you would have to do is change the url of your feed. Enjoy!

<?php
//important to make work with OS X &amp; Linux -- http://www.php.net/manual/en/domdocument.load.php#91384
$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
    )
);
$context = stream_context_create($opts);

libxml_set_streams_context($context);
//read xml feed -- http://www.softarea51.com/tutorials/parse_rss_with_php.html

$doc = new DOMDocument();
	$doc->load('http://www.whatmikelikes.com/blog/feed'); //this is the url for the xml feed from my WordPress Blog
	$arrFeeds = array();
	foreach ($doc->getElementsByTagName('item') as $node) {
		$itemRSS = 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($arrFeeds, $itemRSS);
	}
//read &amp; output the array
	foreach($arrFeeds as $post)
	{
		$title = $post['title'];
		$link = $post['link'];
		$desc = $post['desc'];
		$pubdate = $post['date'];
//convert  $pubdate from Unix timestamp to custom format
$pubdate = date("D, M j Y", strtotime($pubdate));
		$desc = substr($post['desc'],0,160) . "...";
		echo "<span id='title'><a href='$link'>$title</a></span><br/>$desc<br/>posted $pubdate<hr/>"; //output to the screen. HTML added for styling
	}
?>