Motyar

@motyar

Freelance Web Developer

Static Web Hosting, made easy

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,

http://weather.yahooapis.com/forecastrss?w=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 http://weather.yahoo.com/india/delhi/new-delhi-2295019/. 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('http://weather.yahooapis.com/forecastrss?w=20070458&u=c');
 // We need data in return 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER1);

 $output = curl_exec($ch); 
 curl_close($ch);

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

 


Result

The result look like this -


Don't miss Google's Secret Weather API.

Labels: , ,




By :