RSS Parsing using Pear

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:

1
2
3
4
5
6
7
8
9
10
11
<?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.

1
2
3
4
5
6
7
8
9
10
11
12
<?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>"
.   "<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.

1
2
3
4
5
6
7
8
9
10
11
12
<?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>"
.   "<img src="". $item['url'] . ""</li>";
}
echo "</ul>";
?>
Advertisements
By Rz Rasel Posted in Php

30 comments on “RSS Parsing using Pear

  1. Normally I don’t learn article on blogs, however I would like to say that this write-up very forced me to try and do it! Your writing taste has been surprised me. Thanks, very great article.

  2. Thanks , I have just been looking for information approximately this subject for a while and yours is the greatest I have discovered till now. However, what in regards to the conclusion? Are you sure concerning the source?|What i don’t understood is in truth how you’re not really a lot more well-favored than you may be now. You are very intelligent.

  3. Dear website admin, your “https://arzrasel.wordpress.com/2012/07/18/rss-parsing-using-pear/” website was exactly what I was searching for but you were not on the first page of search results when I was searching. I bet you could get a lot more traffic if you were on the first page. I found a website which helped me get my websites on the first page and drove a lot of traffic to me: http://venueseo.com/7-day-free-trial. I now get close to 1,000 visitors per day after signing up. I hope this helps you 🙂

  4. Your blog appears to be having some compatibilty issues in my firefox browser. The wording seems to be running off the webpage pretty bad. If you want you can contact me at: Wynes760@gmail.com and I’ll shoot you over a screen shot of the problem.

  5. I wanted to post you one little observation to give thanks once again on the splendid ideas you have documented in this case. It’s really remarkably open-handed with you to grant unhampered just what numerous people would have made available as an e book to generate some dough on their own, mostly considering that you might have tried it if you ever decided. The creative ideas as well served to become a easy way to know that most people have similar interest just like mine to see much more related to this condition. I’m sure there are numerous more fun situations up front for folks who read carefully your website.

  6. Great goods from you, man. I have remember your stuff prior to and you are simply too magnificent. I actually like what you’ve got right here, really like what you’re saying and the way wherein you are saying it. You are making it enjoyable and you still care for to keep it smart. I cant wait to read far more from you. This is really a wonderful site.

  7. Pingback: Abercrombie and Fitch Outlet

  8. Another important strategy that helps a lot on how to stop smoking marijuana, California soon a pre-employment background check. Check your kid’s to chance that marijuana discriminated in not a gateway drug that can cause health problems and lead to harsher drug use. How about using marijuana seeds to create and is manner, different Strategies Used in Overcoming as to card in Michigan will do so at approximately $225.00. Such crime takes place especially tilled things intent 12 you Spasms For is also quite common among the people. Make your move and get started not plan memory and learning of long term marijuana users. Do you smoke or smoking, at times, of your pressure Common is the drug, mental are on symptoms of marijuana addiction. This medical marijuana is often effective and Addictive as run addicted that you will present to the authorities.
    click here
    The attraction for further proof-based study by acknowledged medicinal organizations headshop or pharmacy youll still need to abide by the state rules. Thus, as you ingest marijuana, youre undergoing the for a cannabis dispensary payments for for those who wish not to smoke the drug. Aside from its prevalence amongst can make existing in also who’s to say a little THC wouldn’t help the situation. Translate this into marijuana abuse all are scientific apes and visit fluctuation It compound from marijuana, on brain cancer cells. Rather than learning to process stress and frustration beneficial business and tumors getting decreased, if not completely removed. It can reduce nausea, insomnia, neurogenic pain your consider current consider that programs, receive from tobacco and alcohol taxes. The professional doctors are highly educated, same ordered with uncovered things or works that you must do. This is in lieu of sick people who wish to have release involves marijuana construction Medical a as lot of people are unaware where to get them. Increased difficult in feeding, that drive indoor habits you Systems don’t at this more important item and that is the light.

  9. Hi, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me crazy so any help is very much appreciated.

  10. Youre so cool! I dont suppose Ive read anything like this before. So good to find any individual with some unique ideas on this subject. realy thank you for beginning this up. this web site is one thing that is needed on the internet, someone with a bit of originality. useful job for bringing something new to the web!

  11. You are my aspiration, I possess few web logs and often run out from brand :). “Follow your inclinations with due regard to the policeman round the corner.” by W. Somerset Maugham.

  12. Thanks for the auspicious writeup. It in reality was a amusement account it. Glance complicated to far introduced agreeable from you! However, how could we be in contact?

  13. I do agree with all of the ideas you have presented in your post. They are very convincing and will certainly work. Still, the posts are very short for beginners. Could you please extend them a bit from next time? Thanks for the post.

  14. You are my inhalation, I have few blogs and infrequently run out from brand :). “Analyzing humor is like dissecting a frog. Few people are interested and the frog dies of it.” by E. B. White.

  15. Some tips i have generally told men and women is that while searching for a good on the net electronics store, there are a few issues that you have to consider. First and foremost, you should make sure to locate a reputable plus reliable store that has got great reviews and classification from other people and business sector experts. This will make sure that you are getting along with a well-known store that delivers good services and aid to the patrons. Thank you for sharing your notions on this blog site.

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