웹프로그래밍

Global It Leader!!


PHP


 
 

기상청 날씨정보 파싱

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 3,356회 작성일 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>

 

댓글목록

등록된 댓글이 없습니다.

전체 81
게시물 검색
PHP 목록
번호 제목 글쓴이 조회 날짜
61 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2460 10-30
60 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4035 10-19
59 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2969 10-22
58 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2914 05-14
57 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3425 02-22
56 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3049 02-22
55 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2868 02-22
열람중 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3357 12-09
53 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3041 09-23
52 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2915 09-21
51 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2950 08-29
50 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2907 08-06
49 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3207 01-04
48 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3163 06-23
47 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3081 04-07
46 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4923 03-19
45 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3145 09-29
44 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3786 06-25
43 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3440 11-11
42 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3722 07-01