RSS Parsing using Pear

Download the packages
wget http://download.pear.php.net/package/XML_RSS-0.9.10.tgz
wget http://download.pear.php.net/package/XML_Parser-1.3.0.tgz

Untar them.
Make a directory named XML inside the public_html directory of the user. Copy the file RSS.php from XML_RSS-0.9.10 and Parser.php from XML_Parser-1.3.0 to the directory XML. Open a test file and paste the php code below. Take in browser and see it works.
safe_mode should be off
==============================

If above does not work try below.

RSS Parsing using Pear
This tutorial will show you how to use Pear (PHP Extension and Application Repository) to parse an RSS feed and display it on your site.

Pear is one of the best hidden jewels inside PHP that programmers usually don’t take advantage of. The PHP Extension and Application Repository is located at here. Pear is described as “a structured library of open-sourced code for PHP users”. If you are running PHP on your box, you probably already have Pear installed! If you are not sure, ask your hosting provider or look at your PHP Info page for a PEAR installation. In this tutorial, we’re going to assume you already have Pear installed on your box.

RSS, or Real Simple Syndication, is a great way to distribute content via XML. For example, you can view the various RSS feeds Yahoo! provides here. In this tutorial, we will use the XML_RSS package from Pear to parse the top Yahoo! Technology News (located at http://rss.news.yahoo.com/rss/tech) and display it on an HTML page.

Step 1 – Install the XML_RSS package
The first thing to do is to install the XML_RSS package from Pear. For this, you will need to SSH or Telnet into your box, or get to a command line where your PHP is installed. If your hosting provider does not give you Telnet or SSH access, ask them to install the XML_RSS Pear package for you. For others who do have access, at the command line, type:

pear install XML_RSS

Executing the previous line will download the XML_RSS and the XML_PARSER modules from the PHP.net website and install it on your machine. You can learn about other PEAR functions at http://www.appelsiini.net/~tuupola/php/peardoc2/installation.cli.html. Now, we are ready to parse the RSS feed using PHP.

Open up a fresh PHP page and copy-paste the code provided below. The code presented below for the RSS feed is a slightly modified code then from the example given on the PEAR site located here:
[php]
<?
require_once “XML/RSS.php”;

$rss =& new XML_RSS(“http://rss.news.yahoo.com/rss/tech”);
$rss->parse();

echo “<ul>\n”;

foreach ($rss->getItems() as $item) {
echo “<li><a href=\”” . $item[‘link’] . “\”>” . $item[‘title’] . “</a></li>\n”;
}

echo “</ul>\n”;
?>
[/php]
The require once line loads the Pear XML_RSS module that we installed earlier. The next two lines loads the XML file from Yahoo and parses it. All the code for parsing the RSS is taken from the PEAR file, so we use it like a blackbox to parse the data. Next, we start the HTML list using the UL tag. The next two lines load each item from the XML file and posts the information on a list. Finally, the last line closes the list that we started. Save your file and point your browser to it. You can see an example of it working by clicking here.

The Pear modules really help you blackbox a lot of things and let you execute tough tasks like parsing an RSS file easily and effeciently. If you have any problems with the code, post about it on the discussion board and we’ll help you out!

Advertisements
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: