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

require_once “XML/RSS.php”;
$cache_file = “/tmp/nlakkakula.wordpress.rss”;
if(!file_exists($cache_file) || (filemtime($cache_file) < time() -86400))
copy(““, $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);
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.


9 Responses to “How to quickly Parse WordPress RSS Feed using PHP”

  1. deniar Says:

    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?

  2. foodscout Says:

    Wow, thank you so much! This is exactly what I needed!

  3. Uroda Says:

    Can I use XML_Parser in someother language then php?

  4. mike Says:

    Here is a simple script that i use for my website
    Hope that helps someone

  5. This pattern offers achieved a new levels thanks to
    involving on-line blog-post products and services you can tackle
    the highway. Since the majority of end users take more time online, sites like make this happen
    end its investigation in overdrive

  6. I very much enjoy your blog right here, thank you so much you have helped me out greatly Grin spread
    the love.

  7. Hi mates, how is everything, and what you wish for to say
    regarding this paragraph, in my view its really awesome in favor of me.

  8. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your website?

    My blog site is in the exact same niche as yours and my users
    would definitely benefit from some of the information you provide here.
    Please let me know if this okay with you. Appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: