A Bit Of XML, RSS And CURL In 7 Lines Of PHP And A Useful Program

Today, I was looking for a quick way to get the current weather information on my computer. There are so many websites out there that offer the information. But I was looking for a program I could permanently install on my computer and launch it whenever I want to lookup the weather information. Oddly, I didn't find any satisfying program. At the same time I was also watching a video about network programming. That inspired me to quickly write a program in PHP to print the current weather information where I live.

I started to look out for a web service that offers information about weather for free. Did I tell you programmableweb.com is a useful website to find web services? If you have subscribed to the Tech Chorus blog you know we've been talking about REST, XML-RPC and web services in general for a while. I landed up on the Yahoo! Weather API web page.

I wrote a program to print the weather information in 7 lines of PHP code. I have published this program on Code Album github repository. You can grab it and use it.

If you want to know how to write similar programs, read on. If you know a bit of PHP and have heard about XML and RSS before you can understand the program and start building upon it.

Yahoo! provides an RSS feed containing the weather information based on a unique identifier for each location. Yahoo! calls this identifier WOEID. You can search your location on the Yahoo! weather page and the redirected URL contains the WOEID for your location. For step by step instructions to find the WOEID for your location, read the information on the Yahoo! weather API page. It turns out the WOEID for my location is 2295420.

The URL http://weather.yahooapis.com/forecastrss?w=2295420&u=c gives detailed weather information about Bangalore. The information is given in RSS format.

The algorithm for our program is

  1. Make a request to http://weather.yahooapis.com/forecastrss?w=2295420&u=c
  2. The response contains the XML document. Store it a variable
  3. Parse the XML document and print the weather information

We can write the fully functional code faster than we could write the pseudo code.

Let's start writing the code.


<?php
$ch 
curl_init();
curl_setopt($chCURLOPT_URL"http://weather.yahooapis.com/forecastrss?w=2295420&u=c");
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$output curl_exec($ch);
curl_close($ch);
?>

The above piece of code uses the PHP curl functions to make a request to http://weather.yahooapis.com/forecastrss?w=2295420&u=c. The response is stored in the variable $output as string. The string is actually the RSS feed and an XML document that conforms to the RSS 2.0 specification.

I am pasting a stripped down version of the response so that you can see the structure of the XML document.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<item>
<title>Conditions for Bangalore, IN at 8:30 pm IST</title>
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/27.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 24 C<BR />
<BR /><b>Forecast:</b><BR />
Wed - Partly Cloudy. High: 30 Low: 19<br />
Thu - Sunny. High: 33 Low: 20<br />
<br />
</item>
</channel>
</rss>

The XML document contains a channel node. The channel node contains the item node. The item node contains the description node. The description node contains the text we want. Given the XML document as string you can grab the description text in just two lines of code.


<?php
$xml 
= new SimpleXMLElement($output);
echo 
$xml->channel->item->description;
?>

The SimeXML PHP extension offers convenient methods to traverse an XML document. You can refer to each node in the XML document as chained methods. In the first line, we instantiate the SimpleXMLElement object and pass the XML document to its constructor. In the second line we traverse the XML document and print the description text.

The output on my computer looks like:

<img src="http://l.yimg.com/a/i/us/we/52/27.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 23 C<BR />
<BR /><b>Forecast:</b><BR />
Wed - Partly Cloudy. High: 30 Low: 19<br />
Thu - Sunny. High: 33 Low: 20<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>

Wiat, Yahoo! decided to put some HTML makrup within the description node in a CDATA section. Let's strip out the HTML tags and print the plain text. Let's make use of the PHP function strip_tags().


<?php
$xml 
= new SimpleXMLElement($output);
echo 
strip_tags($xml->channel->item->description);
?>

Now the output looks like:

Current Conditions:
Mostly Cloudy, 23 C
Forecast:
Wed - Partly Cloudy. High: 30 Low: 19
Thu - Sunny. High: 33 Low: 20
 
Full Forecast at Yahoo! Weather
(provided by The Weather Channel)

That's it. In seven lines of code we've done a bit of XML, RSS and CURL and built a useful program too. Let me know what you think about the whole story.

For your convenience I'm pasting the entire code below.

<?php
$ch 
curl_init();
curl_setopt($chCURLOPT_URL"http://weather.yahooapis.com/forecastrss?w=2295420&u=c");
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$output curl_exec($ch);
curl_close($ch);
echo 
$output;
$xml = new SimpleXMLElement($output);
echo 
strip_tags($xml->channel->item->description);
?>

If you wish to read further about XML and RSS I'd recommend the W3 Schools' XML and RSS tutorials.

Taxonomy upgrade extras: 

Comments

You can get rid of all that curl crap and use file_get_contents, especially since it's a normal GET request with no custom HTTP headers needed


$output = file_get_contents("http://weather.yahooapis.com/forecastrss?w=2295420&u=c");
$xml = new SimpleXMLElement($output);
echo strip_tags($xml->channel->item->description);

You're absolutely right.

file_get_contents() is quite useful for situations like this.

Thanks for the comment.

I was using the file_get_contents to pull weather in and display it on my website. Unfortunately, the host I was using blocked it due to security risks and suggested that I use cURL. So this is just what I was looking for.

Thanks!

Hi All,

cURL has edge over file_get_contents.
It runs faster.
It has many options to manipulate our download (more control).
It is more readable/maintainable.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.