Creating a PHP Feed for a Blog on your Website
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 & 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 & 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 } ?>
| Print article | This entry was posted by Mike on August 26, 2010 at 12:55 pm, and is filed under PHP, Web Programming. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
To integrate synamic functionalities in websites, PHP is one application that needs to be learned. It is good that there are sites that offer PHP programming consultancies or programming services. With these services, you can easily upgrade your site to a more dynamic one.
about 1 year ago
I guess I agree with what you are saying although I don’t want too :p
about 1 year ago
There is clearly a whole lot to understand about this. I believe you produced some beneficial factors in Characteristics also.
Keep operating , good occupation!
about 1 year ago
Most of the times blogs are the same but i think that your blog can be an exception. Bravo !