웹프로그래밍

Global It Leader!!


PHP


 
 

기상청 날씨정보 파싱

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 3,344회 작성일 18-12-09 21:26

본문

Answer
Go to your php.ini file and remove the ; mark from the beginning of the following line:
;extension=php_curl.dll

기상청 주간날씨 정보 xml 링크
http://www.kma.go.kr/weather/lifenindustry/sevice_rss.jsp

현재 날씨 정보 xml (이건 이런식으로 파싱해서 사용)
http://www.kma.go.kr/XML/weather/sfc_web_map.xml
*/

/* 지역별 xml URL
서울·경기도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=109
강원도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=105
충청북도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=131
충청남도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=133
전라북도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=146
전라남도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=156
경상북도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=143
경상남도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=159
제주특별자치도 http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=184
*/

function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

$url = "http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=109";
$xml_string = file_get_contents_curl($url);
$enc = mb_detect_encoding($xml_string, array('EUC-KR', 'UTF-8', 'shift_jis', 'CN-GB'));
if($enc != 'UTF-8'){
$xml_string = iconv($enc, 'UTF-8', $xml_string); //본인 사이트에 맞는 인코딩으로 변경
}
$result = simplexml_load_string($xml_string);

$list = array();
//echo $result->body->location->data[0]->tmn;
$location= iconv("UTF-8","euc-kr",$result->header->title); //예보지역
$results = $result->body->location;
foreach($results->data as $item)
{
$num = $item->numEf; //n 일후 예보
$wdate = $item->tmEf; // 날짜
$wformat =iconv("UTF-8","euc-kr",$item->wf); //날씨, (맑음,구름조금,구름많음,흐림,비,눈/비,눈
$tmin = $item->tmn; //최저온도
$tmax = $item->tmx; //최고온도
$rainrate =iconv("UTF-8","euc-kr",$item->reliability); //신뢰도
$tmparr = array($num, $wdate, $wformat, $tmin, $tmax, $rainrate);
array_push($list, $tmparr);
}
?>
<html>
<body>
<?
echo "<b>".$location." </b><br>";
if (isset($list))
{
for($i = 0; $i < count($list); $i++)
{
$num = $list[$i][0];
$wdate = $list[$i][1];
$wformat = $list[$i][2];
$tmin = $list[$i][3];
$tmax = $list[$i][4];
$rainrate = $list[$i][5];
?>
<?=$num?>일후 예보, 날짜 : <?=$wdate?>,
날씨 : <?=$wformat?>, 최저 : <?=$tmin?>도,
최고 : <?=$tmax?>도, 신뢰도 : <?=$rainrate?><br><br>
<?
}
}
?>
</body>

 

댓글목록

등록된 댓글이 없습니다.

전체 80
게시물 검색
PHP 목록
번호 제목 글쓴이 조회 날짜
40 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3868 12-31
39 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3824 04-11
38 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3796 01-23
37 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3777 06-25
36 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3713 07-01
35 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3691 08-28
34 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3543 03-26
33 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3429 11-11
32 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3417 02-22
열람중 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3345 12-09
30 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3196 01-04
29 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3155 06-23
28 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3137 09-29
27 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3073 04-07
26 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3041 02-22
25 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3032 09-23
24 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2961 10-22
23 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2942 08-29
22 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2919 03-30
21 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2910 09-21