-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgetWeather.php
More file actions
72 lines (52 loc) · 1.96 KB
/
Copy pathgetWeather.php
File metadata and controls
72 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/*
Date: 2016-01-28
Author: skkong89@gmail.com
ex) http://angel.vps.phps.kr/data_analysis/getWeather.php?yy=2013&mm=1&stn=108
ex) http://www.kma.go.kr/weather/climate/past_cal.jsp?stn=108&yy=2013&mm=1&x=23&y=8&obs=1
*/
// 날씨 조회 api
function getWeather($yy, $mm, $stn)
{
$arr_data = array();
$url = "http://www.kma.go.kr/weather/climate/past_cal.jsp?stn=$stn&yy=$yy&mm=$mm&obs=1&x=24&y=9";
$results = "";
$contents = file_get_contents($url);
if($contents)
{
// euc-kr 페이지를 utf8로 변환한다.
$contents = iconv('euc-kr', 'utf8', $contents);
}
// <td class="align_left">평균기온:2.2℃<br />최고기온:3.7℃<br />최저기온:-0.3℃<br />평균운량:9.6<br />일강수량:0.0mm<br /></td>
$regex = '/.*<td class="align_left">평균기온:(.*?)<br \/>최고기온:(.*?)<br \/>최저기온:(.*?)<br \/>평균운량:(.*?)<br \/>일강수량:(.*?)<br \/><\/td>.*/';
// 라인단위로 분리
$arr_line = explode("\n", $contents);
$day = 1; // 일 정보
foreach($arr_line as $line)
{
if(strpos($line, "평균기온") > 0)
{
$line = str_replace("℃", "", $line);
$line = str_replace("mm", "", $line);
preg_match ($regex , $line, $matches);
$arr_data[$day]['avg'] = $matches[1]; // 평균기온
$arr_data[$day]['high'] = $matches[2]; // 최고기온
$arr_data[$day]['low'] = $matches[3]; // 최저기온
$arr_data[$day]['cloud'] = $matches[4]; // 평균운량
$arr_data[$day]['rain'] = trim(str_replace("-", '0', $matches[5])); // 일강수량
$day++;
}
}
return $arr_data;
}
// main routine
// ====================
$yy = $_REQUEST['yy']; // 년도
$mm = $_REQUEST['mm']; // 월
$stn = $_REQUEST['stn']; // 지역
$yy = $yy == '' ? date("Y") : $yy;
$mm = $mm == '' ? date('m') : $mm;
$stn = $stn == '' ? 108 : $stn;
$arr_data = getWeather($yy, $mm, $stn);
echo json_encode($arr_data);
?>