yahoo weather API sample

/*

Example: Yahoo! Weather API Example Using SimpleXML in PHP.

The base URL for the Weather RSS feed is
http://weather.yahooapis.com/forecastrss

Parameters
w  WOEID 
e.g: w=111111

u  Units for temperature
f: Fahrenheit
c: Celsius 
e.g.: u=c

*/


$city="taipei-city";

$result1 = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22$city%22&format=xml");
$xml1 = simplexml_load_string($result1);
 $woeid=$xml1->results->place->woeid;

if($woeid!="")
{
 $fetchData = file_get_contents("http://weather.yahooapis.com/forecastrss?w=$woeid&u=c");
 $xmlData = simplexml_load_string($fetchData);
 $location = $xmlData->channel->xpath('yweather:location');
 

 if(!empty($location))
 {
 
   foreach($xmlData->channel->item as $data)
   {
   $current_condition = $data->xpath('yweather:condition');
   $forecast = $data->xpath('yweather:forecast');
    $current_condition = $current_condition[0];
   echo "
   

{$location[0]['city']}, {$location[0]['region']}

Date: {$current_condition['date']}

Current Temprature

{$current_condition['temp']}°C {$current_condition['text']}

Forecast

{$forecast[0]['day']} : {$forecast[0]['text']}. High: {$forecast[0]['high']} Low: {$forecast[0]['low']}
{$forecast[1]['day']} - {$forecast[1]['text']}. High: {$forecast[1]['high']} Low: {$forecast[1]['low']}
"; } } else { echo "

please try a different City.

"; } } else { echo "

Please try a different City.

"; }

畫面呈現

Previous
Next Post »