Google weather API
php 5.2 이상 - simplexml_load_string() 지원가능해야 함
function google_weather($city) { $xml_file = "http://www.google.co.kr/ig/api?weather=".$city; //request url $get_file = file_get_contents($xml_file); if ($get_file === false) { die('Request failed1'); } $xmlObj = iconv("EUC-KR","UTF-8", $get_file); //only utf-8 $parser = simplexml_load_string($xmlObj); if ($parser === false) { die('Request failed2'); } /* forecast_information */ $city = $parser->weather->forecast_information->postal_code->attributes()->data; $forecast_date = $parser->weather->forecast_information->forecast_date->attributes()->data; //current_conditions : 현재날씨 $condition = $parser->weather->current_conditions->condition->attributes()->data; //날씨 $temp_f = $parser->weather->current_conditions->temp_f->attributes()->data."℉"; //온도(화씨) ℉ = °F $temp_c = $parser->weather->current_conditions->temp_c->attributes()->data."℃"; //온도(섭씨) ℃ = °C $humidity = $parser->weather->current_conditions->humidity->attributes()->data; //습도 $icon = $parser->weather->current_conditions->icon->attributes()->data; //아이콘 $icons = "http://www.google.co.kr".$icon; $wind_condition = $parser->weather->current_conditions->wind_condition->attributes()->data; //풍량 $current_arr = array($city,$forecast_date,$condition,$temp_f,$temp_c,$humidity,$icons,$wind_condition); /* forecast_conditions */ /* count($forecast_conditions) => 4 : 오늘,내일,모레,글피 */ for($i=0; $i < count($parser->weather->forecast_conditions); $i++) { $day_of_week = $parser->weather->forecast_conditions[$i]->day_of_week->attributes()->data; //요일 $low = $parser->weather->forecast_conditions[$i]->low->attributes()->data."℃"; //최저온도 $high= $parser->weather->forecast_conditions[$i]->high->attributes()->data."℃"; //최고온도 $icon = $parser->weather->forecast_conditions[$i]->icon->attributes()->data; //아이콘 $icons = "http://www.google.co.kr".$icon; $condition= $parser->weather->forecast_conditions[$i]->condition->attributes()->data; //날씨 $forecast_arr[$i] = array($day_of_week,$low,$high,$icons,$condition); } return array($current_arr,$forecast_arr[0],$forecast_arr[1],$forecast_arr[2],$forecast_arr[3]); }
'API' 카테고리의 다른 글
Google Maps 좌표 검색하기 (0) | 2012.10.10 |
---|---|
Google Maps Javascript API V3 (Last updated 2012.04.16) (1) | 2012.08.30 |