go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

RSS Parsing using Pear

Tuesday, 6th December, 2011  /  PHP Tutorials  /  2 Comments
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>";
?>
Share on:

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

    Please, be polite and helpful and do not spam or offend others! We promise you will be treated the same way!

    Log in to your account to post your comments. If you still haven't joined our community yet, you can create your FREE account now!

    Posting tip:
    If you use code in your comments, please put it in these tags [php], [sql], [css], [js] PHP code example: [php] echo date("Y-m-d"); [/php]

    Thank you,
    PHPJabbers Team

    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