How to quickly Parse WordPress RSS Feed using PHP
June 3, 2007
The easy way to parse RSS (Really Simple Syndication) fee from wordpress blog in your personal site using PHP is with PEAR:XML_RSS package.
Installing XML_RSS
Step1 : run Pear install XML_RSS
The chances are you endup with an error saying XML_RSS requires XML_Tree….. so run “pear install –alldeps XML_RSS’ this will download XML_tree….. But later on you might found out that XML_RSS is not working complaining about XML_parser is not found. I blindly assumed that –alldeps will takecare that but it didnt. so you need to do “pear install XML_Parser” also. So in short…..
pear install XML_Parser
pear install XML_Tree
pear install XML_RSS
Step 2: Grab RSS Feed from wordpress and copy to your local disk
There is no point in fetching RSS and parsing when every time user hits or refreshes your webpage as the content is not that dynamic (atleast for me). So I would cache the rss feed and refresh every 24 hrs.
So the php code goes like
<?php
require_once “XML/RSS.php”;
$cache_file = “/tmp/nlakkakula.wordpress.rss”;
if(!file_exists($cache_file) || (filemtime($cache_file) < time() -86400))
{
copy(“http://nlakkakula.wordpress.com/feed“, $cache_file);
}
Step 3: Parsing the Feed
XML_RSS has getItems() method, which grabs all items into an array to make your life easy.
so the rest of the code is
$r = & new XML_RSS($cache_file);
$r->parse();
echo “<h3> Recent Blog Entries From WordPress </h3>”;
echo “<ul>\n”;
foreach ($r->getItems() as $entry)
{
echo “<li><a href=\”".$entry['link'].”\”>”.$entry['title'].”</a></li>\n”;
}
echo “</ul>\n”;
?>
This simple PHP scriptlet grabs WordPress feed and shows the items in your website.
September 10, 2007 at 4:39 pm
Thanks for tip!
June 15, 2008 at 2:35 am
How we can it grab the string inside the tag ?
I’ve made some script too, I can grab the string inside and but i have some difficulty on grab the string inside . Can U help me?
Thanks
July 4, 2008 at 3:28 pm
Wow, thank you so much! This is exactly what I needed!