go top

MEGA Sale

  • Colection of 65 PHP scripts for $6 each

RSS Parsing using Pear

Limited offer!

Get a huge collection of PHP scripts in a special bundle!

Mega Sale Price
This tutorial aims to show you how to use PEAR to parse RSS feed on a PHP page. For this tutorial we will use the XML_RSS package from Pear to parse the top news stories at Yahoo!. The RSS feed we will be using is http://rss.news.yahoo.com/rss/topstories. To begin you will need to have XML_RSS installed. To install this package you will need to have telnet, or more commonly ssh access to the web server host. If you are on a shared service provider, you may need to contact your hosting company for this step. To install XML_RSS simply run:

pear install XML_RSS
This will download and install the required packages. If you do not have ssh access and your hosting provider is unwilling to install this pear package, you may download it yourself and store it locally in web root somewhere. The examples provided will still work, you'll simply need to replace the include path with the full path to your pear XML_RSS files.

Now that you have XML_RSS installed you can begin parsing RSS feeds. Begin by creating a new PHP page, we will call ours news.php. Include the following:

<?php
require_once "XML/RSS.php";
$rss =& new XML_RSS("http://rss.news.yahoo.com/rss/topstories");
$rss->parse();
echo "<h1>Headlines from <a href="http://yahoo.com">Yahoo!</a></h1>";
echo "<ul>";
foreach ($rss->getItems() as $item) {
echo "<li><a href="" . $item['link'] . "">" . $item['title'] . "</a></li>";
}
echo "</ul>";
?>


In the example above we're pulling the stories listed on the top news stories page for Yahoo! news. This example will return a list with the link and title for each story. Keep in mind RSS feeds may contain other valuable information for your site as well. Many contain photos and descriptions as well. In the next example we will use the getImages() function to display graphics as well. Again open a new PHP file, we will call this one news-images.php.

<?php
require_once "XML/RSS.php";
$rss =& new XML_RSS("http://rss.news.yahoo.com/rss/topstories");
$rss->parse();
echo "<h1>Headlines from <a href="http://yahoo.com">Yahoo!</a></h1>";
echo "<ul>n";
foreach ($rss->getImages() as $item) {
echo "<li><a href="" . $item['link'] . "">" . $item['title'] . "</a><br>"
. "<img src="". $item['url'] . ""</li>";
}
echo "</ul>";
?>


One common problem with RSS feeds is the latency it takes between the sites exchanging the data. This can be overcome with a simple cron script, and then taking the RSS feed locally instead of remote. This is much faster because each time a user opens this script as is your server is making another connection out to each hosts you're getting an RSS feed from. If that host is experiencing problems, or is just really slow, your viewers are going to experience the same on your site. An example might be:

0 * * * * /usr/bin/wget -q -O /tmp/yahoo.xml http://rss.news.yahoo.com/rss/topstories
The above will save a local copy of the RSS feed to the directory /tmp every hour. You can run this more often if you want. Of course you;ll need to make a change to the script. Below is how you'll do this.

<?php
require_once "XML/RSS.php";
$rss =& new XML_RSS("/tmp/yahoo.xml");
$rss->parse();
echo "<h1>Headlines from <a href="http://yahoo.com">Yahoo!</a></h1>";
echo "<ul>";
foreach ($rss->getImages() as $item) {
echo "<li><a href="" . $item['link'] . "">" . $item['title'] . "</a><br>"
. "<img src="". $item['url'] . ""</li>";
}
echo "</ul>";
?>

Special Offer!

Get all 65 PHP scripts in a bundle for $6 each only!

Mega Sale Offer

2 Comments to "RSS Parsing using Pear"

  • keith

    keith

    January 25, 2014 at 22:17 pm

    There is an error on this second script above.


    Deprecated: Assigning the return value of new by reference is deprecated in /home/content/rss/html/index.php on line 3

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/content/rss/html/index.php on line 5

  • aneeeq

    aneeeq

    April 24, 2012 at 08:32 am

    There are several ways to read RSS feed in PHP, but this one is surely one of the easiest.

    <?php

    $feed = file_get_contents(‘http://www.mywebsite.com/rss/’);
    $rss = new SimpleXmlElement($feed);

    foreach($rss->channel->item as $entry) {
    echo “<p><a href=’$entry->link’ title=’$entry->title’>” . $entry->title . “</a></p>”;
    }

    ?>

Add your comment

Captcha
    • PHP Scripts

      Check our extensive collection of top-notch PHP Scripts that will enhance your website!

      Commercial PHP scripts
    • Free Scripts

      Add great new functionalities to your website with our Free Scripts collection.

      Free scripts