« How to send email with PHPCreate PDF file with PHP »

RSS Parsing using Pear Posted in PHP | 1 Comment
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>";
?>

Do you know PHP / HTML / CSS / JS well?

Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your knowledge with thousands of webmasters and we will reward you for your generosity by giving you bonus points which you can use as a voucher to buy any of our commercial products. Read more about our reward program.

1 Reply to "RSS Parsing using Pear"

aneeeq April 24, 2012 at 8:32 am | Reply

0

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>”;
}

?>
  • 1


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

Log in your free account or if you still haven't joined our Webmaster Community Reward Program, 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 ~