Freelance Web Developer

Say Hi

Apr 11, 2010

Weather forecast php application in 7 lines

Today, I was looking for a way ( API etc )to get the current weather information for my application. And i found Yahoo! Weather. The Weather RSS feed provide weather information for your location. You can incorporate the RSS data into your application. The Weather RSS feed is a based on WOEID.

Preparing Request

For the Weather RSS feed there are two parameters:
1. w for WOEID.
2. u for degrees units (Fahrenheit or Celsius).

Getting WOEID
WOEID indicates the location,

To find your WOEID, browse or search for your city from the Weather home page. The WOEID is in the URL for the forecast page for that city. For example, if you search for The Weather Channel New Delhi on the Weather home page, the forecast page for that city is The WOEID is 2295019.

Our Application

In our application we are fetching the data from the Yahoo! Weather RSS, with help of cURL. And will show the response by parsing SimpleXMLElement

 //init cURL
 $ch = curl_init('');
 // We need data in return 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER1);

 $output = curl_exec($ch); 

 //parsing data
 $xml = new SimpleXMLElement($output);
 echo '<h1>'.$xml->title.'</h1>';
 echo ($xml->item->description); 



The result look like this -

Don't miss Google's Secret Weather API.

Labels: , ,

By :