[소스] 구글 날씨 붙이기 정보
[소스] 구글 날씨 붙이기
관련링크
첨부파일
본문
<?
/*
작성: freeimage.kr 김성대 2009-01-16
내용: 구글 API 중 날씨 부분을 가져온다.
구글날씨 관련 저작권은 구글에서 찾아보기 바람.. (비상용이면 문제없을듯함..)
$g4[weater_cache_file] 파일을 지정하면 캐시방법으로 1시간마다 한번씩 갱신한다.
주소창에 weather.php?reget=1 식으로 입력하면 캐시를 무시하고 읽어온다..
*/
include_once("./_common.php");
include_once("$g4[path]/lib/latest.lib.php");
$g4['title'] = "";
include_once("./_head.php");
$g4[weater_cache_file]="../../tmp/weater_cache.txt";
?>
<table width=100% cellpadding="0" cellspacing="1" bgcolor=#A0A0A0>
<tr align=center bgcolor=#F0F0F0>
<td>지역</td>
<td>현재</td>
<td colspan=4>예보</td>
</tr>
<?
if ($g4[weater_cache_file]&&!$_GET[reget]&&@filemtime($g4[weater_cache_file])>time()-3600) {
echo file_get_contents($g4[weater_cache_file]);
}
else {
if ($g4[weater_cache_file]) {
ob_start();
}
// 원하는 지역만 넣으세요.. $arr1=explode("|","서울|대전|대구|부산|광주|인천|군산|전주|울산|제주|진주|포항|안동|청주|춘천|강릉|마산|목포|상주|서귀포|속초|수원|완도|원주|여수|예천|New York City|Los Angeles|Chicago|Houston|Phoenix|Philadelphia|San Antonio|San Diego|Dallas|San Jose");
$arr1=explode("|","서울|대전|대구|부산|광주|인천|울산|제주|춘천|원주");
foreach($arr1 as $area) {
//if ($area!="울산") continue;
$weater=get_weather($area);
if (!$weater) continue;
?>
<tr align=center bgcolor=#FFFFFF>
<td><?=$area?></td>
<td><?=make_weater($weater[0])?></td>
<td><?=make_weater($weater[1])?></td>
<td><?=make_weater($weater[2])?></td>
<td><?=make_weater($weater[3])?></td>
<td><?=make_weater($weater[4])?></td>
</tr>
<?
}
if ($g4[weater_cache_file]) {
$wr_content = ob_get_contents();
ob_end_clean();
file_put_contents($g4[weater_cache_file],$wr_content);
echo $wr_content;
}
}
?>
</table>
<?
function make_weater($arr) {
if (!$arr) return " ";
$arr[4]=str_replace("'","",$arr[4]);
$out.="<img src='http://www.google.com"."$arr[4]' alt='날씨: $arr[1] $arr[7]'><BR>";
if ($arr[0]=="현재") $out.="$arr[5]°$arr[6]<BR>";
else $out.="$arr[0] $arr[2]°~$arr[3]°";
return $out;
}
function weatherParser($items, $str) {
$result = Array();
foreach($items as $item) {
if (eregi("<$item data=\"([^\"]+)\"/>", $str, $reg))
$result[$item] = $reg[1];
else
$result[$item] = "";
}
return $result;
}
function get_weather($area) {
$arr1=explode("|","
서울|대전|대구|부산|광주|
인천|군산|전주|울산|제주|
진주|포항|안동|청주|춘천|
강릉|마산|목포|상주|서귀포|
속초|수원|완도|원주|여수|
예천|파주|김해|안산|익산|용인|
New York City|Los Angeles|Chicago|Houston|Phoenix|
Philadelphia|San Antonio|San Diego|Dallas|San Jose");
$arr2=explode("|","
Seoul|Taejon|Taegu|Busan|Kwangju|
Inchon|Kunsan|Jeonju|Ulsan|Jeju|
Jinju|Pohang|Andong|Cheongju|Chuncheon|
Kangnung|Masan|Mokpo|Sangju|Seogwipo|
Sokcho|Suwon|Wando|Wonju|Yosu|
Yecheon|Paju|Gimhae|Ansan|Iksan|Yongin|
New York City|Los Angeles|Chicago|Houston|Phoenix|
Philadelphia|San Antonio|San Diego|Dallas|San Jose");
for($i=0;$i<sizeof($arr1);$i++) {
if (trim($arr1[$i])==$area) {
$area2=trim($arr2[$i]);
break;
}
}
//echo "$area -> $area2 ($i) \n";
$area2=str_replace(" ","%20",$area2);
if (!$area2) return "";
// EUC-KR 환경이면 "&oe=UTF-8"를 빼주세요.
$xml_data= @file_get_contents("http://www.google.com/ig/api?hl=ko&weather=$area2&oe=UTF-8");
preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $xml_data, $reg, PREG_SET_ORDER);
$weater=array();
$current_data = $reg[1][1];
$count=0;
$count_limit=5;
if ($current_data != '') {
$wdata = Array();
$day_data = $reg[2][1];
if ($day_data != '')
$wdata['day0'] = weatherParser(Array('day_of_week','low','high','icon','condition'), $day_data);
$wdata['current'] = weatherParser(Array('condition','temp_f','temp_c','humidity','icon','wind_condition'), $current_data);
//var_dump($wdata['current']);
if (!$wdata['current']['icon']) $wdata['current']['icon']=$wdata['day0']['icon'];
//weater(요일,날씨,최저,최고,아이콘,현재온도,습도,바람) // 현재온도,습도,바람 는 현재에만 해당됨
$count++;
$weater[]=array('현재',$wdata['current']['condition'],$wdata['day0']['low'],$wdata['day0']['high'],$wdata['current']['icon'],$wdata['current']['temp_c'],$wdata['current']['humidity'],$wdata['current']['wind_condition']);
if ($wdata['day0']&&$count++<$count_limit) {
$weater[]=array($wdata['day0']['day_of_week'],$wdata['day0']['condition'],$wdata['day0']['low'],$wdata['day0']['high'],$wdata['day0']['icon']);
}
$day_data = $reg[3][1];
if ($day_data != '') {
if ($count++<$count_limit) {
$wdata = weatherParser(Array('day_of_week','low','high','icon','condition'), $day_data);
$weater[]=array($wdata['day_of_week'],$wdata['condition'],$wdata['low'],$wdata['high'],$wdata['icon']);
}
}
$day_data = $reg[4][1];
if ($day_data != '') {
if ($count++<$count_limit) {
$wdata = weatherParser(Array('day_of_week','low','high','icon','condition'), $day_data);
$weater[]=array($wdata['day_of_week'],$wdata['condition'],$wdata['low'],$wdata['high'],$wdata['icon']);
}
}
$day_data = $reg[5][1];
if ($day_data != '') {
if ($count++<$count_limit) {
$wdata = weatherParser(Array('day_of_week','low','high','icon','condition'), $day_data);
$weater[]=array($wdata['day_of_week'],$wdata['condition'],$wdata['low'],$wdata['high'],$wdata['icon']);
}
}
//echo "<pre>"; print_r($wdata);
}
return $weater;
}
?>
<?
include_once("./_tail.php");
?>
8
댓글 10개



아래는 영문데이타입니다.
$area_arr = Array('Seoul'=>'서울','Songnam'=>'성남','Inchon'=>'인천','Suwon'=>'수원','Anyang'=>'안양','Ansan'=>'안산','Osan'=>'오산','Bucheon'=>'부천','Uijongbu'=>'의정부','Pyongtaek'=>'평택','Yongin'=>'용인','Gunpo'=>'군포','Chuncheon'=>'춘천','Wonju'=>'원주','Tonghae'=>'동해','Samchok'=>'삼척','Kangneung'=>'강릉','Cheongju'=>'청주','Chungju'=>'충주','Daejeon'=>'대전','Taean'=>'태안','Cheonan'=>'천안','Dangjin'=>'당진','Jeonju'=>'전주','Kunsan'=>'군산','Iksan'=>'익산','Kwangju'=>'광주','Mokpo'=>'목포','Yeosu'=>'여수','Sunchon'=>'순천','Daegu'=>'대구','Pohang'=>'포항','Sangju'=>'상주','Andong'=>'안동','Gumi'=>'구미','Pusan'=>'부산','Ulsan'=>'울산','Masan'=>'마산','Jinju'=>'진주','Jinhae'=>'진해','Geoje-si'=>'거제','Gimhae'=>'김해','Changwon'=>'창원','Yangsan'=>'양산','Ulleung-gun'=>'울릉도','Jeju'=>'제주');
