웹프로그래밍

Global It Leader!!


PHP


 
 

기상청 날씨정보 파싱

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 3,340회 작성일 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 목록
번호 제목 글쓴이 조회 날짜
80 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1366 03-29
79 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3542 03-26
78 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1535 03-26
77 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1384 03-17
76 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1951 11-28
75 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2154 08-11
74 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1888 08-10
73 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2486 08-02
72 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2561 07-20
71 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1857 07-05
70 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2155 05-27
69 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2916 03-30
68 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2208 03-30
67 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2146 03-29
66 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2139 03-27
65 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2723 03-10
64 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2847 02-24
63 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2200 02-22
62 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3867 12-31
61 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2448 10-30