데이터 처리를 하려다보니 공휴일 정보가 필요했다. 토요일, 일요일이나 법정공휴일은 날짜가 지정되어 있으니 지정할 수 있지만 대체공유일아니 음력기반 공휴일들은 뽑을 수가 없다. 물론 음력기반 공휴일은 검색해보니 있는것같긴 한데 굳이 그렇게 쓸 필요가 있나 싶다.
그렇다고 해마다 달력을 받아서 그때그때 때려 맞춰서 하는 방법은 세상 무식한 방법이고해서 찾아보니 공공데이터포털에서 제공해주는 `한국천문연구원_특일 정보` API가 있었다. 이 API는 공휴일 뿐만 아니라 더 많은 데이터를 제공해주니 필요하다면 공공데이터 포털 사이트 가입 후 이용해보길 바란다.
(우스개 소리로 네이버 날씨가 기상청보다 정확한면이 있는데 아무래도 네이버에서 그때그때 실제 날씨를 보고 때려맞춰서 변경하는게 아닌가 싶다.)
우선 제공되는 데이터는 JSON 데이터로 만들었으며 하단 첨부파일 또는 소스코드를 복사해서 사용하면 된다.
{"all":["2023-01-01","2023-01-21","2023-01-22","2023-01-23","2023-01-24","2023-03-01","2023-05-05","2023-05-27","2023-05-29","2023-06-06","2023-08-15","2023-09-28","2023-09-29","2023-09-30","2023-10-03","2023-10-09","2023-12-25","2024-01-01","2024-02-09","2024-02-10","2024-02-11","2024-02-12","2024-03-01","2024-04-10","2024-05-05","2024-05-06","2024-05-15","2024-06-06","2024-08-15","2024-09-16","2024-09-17","2024-09-18","2024-10-03","2024-10-09","2024-12-25"],"detail":{"2023-01-01":{"dateKind":"01","dateName":"1\uc6d41\uc77c","isHoliday":"Y","locdate":20230101,"seq":1},"2023-01-21":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20230121,"seq":1},"2023-01-22":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20230122,"seq":1},"2023-01-23":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20230123,"seq":1},"2023-01-24":{"dateKind":"01","dateName":"\ub300\uccb4\uacf5\ud734\uc77c","isHoliday":"Y","locdate":20230124,"seq":1},"2023-03-01":{"dateKind":"01","dateName":"\uc0bc\uc77c\uc808","isHoliday":"Y","locdate":20230301,"seq":1},"2023-05-05":{"dateKind":"01","dateName":"\uc5b4\ub9b0\uc774\ub0a0","isHoliday":"Y","locdate":20230505,"seq":1},"2023-05-27":{"dateKind":"01","dateName":"\ubd80\ucc98\ub2d8\uc624\uc2e0\ub0a0","isHoliday":"Y","locdate":20230527,"seq":1},"2023-05-29":{"dateKind":"01","dateName":"\ub300\uccb4\uacf5\ud734\uc77c","isHoliday":"Y","locdate":20230529,"seq":1},"2023-06-06":{"dateKind":"01","dateName":"\ud604\ucda9\uc77c","isHoliday":"Y","locdate":20230606,"seq":1},"2023-08-15":{"dateKind":"01","dateName":"\uad11\ubcf5\uc808","isHoliday":"Y","locdate":20230815,"seq":1},"2023-09-28":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20230928,"seq":1},"2023-09-29":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20230929,"seq":1},"2023-09-30":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20230930,"seq":1},"2023-10-03":{"dateKind":"01","dateName":"\uac1c\ucc9c\uc808","isHoliday":"Y","locdate":20231003,"seq":1},"2023-10-09":{"dateKind":"01","dateName":"\ud55c\uae00\ub0a0","isHoliday":"Y","locdate":20231009,"seq":1},"2023-12-25":{"dateKind":"01","dateName":"\uae30\ub3c5\ud0c4\uc2e0\uc77c","isHoliday":"Y","locdate":20231225,"seq":1},"2024-01-01":{"dateKind":"01","dateName":"1\uc6d41\uc77c","isHoliday":"Y","locdate":20240101,"seq":1},"2024-02-09":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20240209,"seq":1},"2024-02-10":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20240210,"seq":1},"2024-02-11":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20240211,"seq":1},"2024-02-12":{"dateKind":"01","dateName":"\ub300\uccb4\uacf5\ud734\uc77c(\uc124\ub0a0)","isHoliday":"Y","locdate":20240212,"seq":1},"2024-03-01":{"dateKind":"01","dateName":"\uc0bc\uc77c\uc808","isHoliday":"Y","locdate":20240301,"seq":1},"2024-04-10":{"dateKind":"01","dateName":"\uad6d\ud68c\uc758\uc6d0\uc120\uac70","isHoliday":"Y","locdate":20240410,"seq":1},"2024-05-05":{"dateKind":"01","dateName":"\uc5b4\ub9b0\uc774\ub0a0","isHoliday":"Y","locdate":20240505,"seq":1},"2024-05-06":{"dateKind":"01","dateName":"\ub300\uccb4\uacf5\ud734\uc77c(\uc5b4\ub9b0\uc774\ub0a0)","isHoliday":"Y","locdate":20240506,"seq":1},"2024-05-15":{"dateKind":"01","dateName":"\ubd80\ucc98\ub2d8\uc624\uc2e0\ub0a0","isHoliday":"Y","locdate":20240515,"seq":1},"2024-06-06":{"dateKind":"01","dateName":"\ud604\ucda9\uc77c","isHoliday":"Y","locdate":20240606,"seq":1},"2024-08-15":{"dateKind":"01","dateName":"\uad11\ubcf5\uc808","isHoliday":"Y","locdate":20240815,"seq":1},"2024-09-16":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20240916,"seq":1},"2024-09-17":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20240917,"seq":1},"2024-09-18":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20240918,"seq":1},"2024-10-03":{"dateKind":"01","dateName":"\uac1c\ucc9c\uc808","isHoliday":"Y","locdate":20241003,"seq":1},"2024-10-09":{"dateKind":"01","dateName":"\ud55c\uae00\ub0a0","isHoliday":"Y","locdate":20241009,"seq":1},"2024-12-25":{"dateKind":"01","dateName":"\uae30\ub3c5\ud0c4\uc2e0\uc77c","isHoliday":"Y","locdate":20241225,"seq":1}}}
<?php
$d = '{"all":["2023-01-01","2023-01-21","2023-01-22","2023-01-23","2023-01-24","2023-03-01","2023-05-05","2023-05-27","2023-05-29","2023-06-06","2023-08-15","2023-09-28","2023-09-29","2023-09-30","2023-10-03","2023-10-09","2023-12-25","2024-01-01","2024-02-09","2024-02-10","2024-02-11","2024-02-12","2024-03-01","2024-04-10","2024-05-05","2024-05-06","2024-05-15","2024-06-06","2024-08-15","2024-09-16","2024-09-17","2024-09-18","2024-10-03","2024-10-09","2024-12-25"],"detail":{"2023-01-01":{"dateKind":"01","dateName":"1\uc6d41\uc77c","isHoliday":"Y","locdate":20230101,"seq":1},"2023-01-21":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20230121,"seq":1},"2023-01-22":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20230122,"seq":1},"2023-01-23":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20230123,"seq":1},"2023-01-24":{"dateKind":"01","dateName":"\ub300\uccb4\uacf5\ud734\uc77c","isHoliday":"Y","locdate":20230124,"seq":1},"2023-03-01":{"dateKind":"01","dateName":"\uc0bc\uc77c\uc808","isHoliday":"Y","locdate":20230301,"seq":1},"2023-05-05":{"dateKind":"01","dateName":"\uc5b4\ub9b0\uc774\ub0a0","isHoliday":"Y","locdate":20230505,"seq":1},"2023-05-27":{"dateKind":"01","dateName":"\ubd80\ucc98\ub2d8\uc624\uc2e0\ub0a0","isHoliday":"Y","locdate":20230527,"seq":1},"2023-05-29":{"dateKind":"01","dateName":"\ub300\uccb4\uacf5\ud734\uc77c","isHoliday":"Y","locdate":20230529,"seq":1},"2023-06-06":{"dateKind":"01","dateName":"\ud604\ucda9\uc77c","isHoliday":"Y","locdate":20230606,"seq":1},"2023-08-15":{"dateKind":"01","dateName":"\uad11\ubcf5\uc808","isHoliday":"Y","locdate":20230815,"seq":1},"2023-09-28":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20230928,"seq":1},"2023-09-29":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20230929,"seq":1},"2023-09-30":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20230930,"seq":1},"2023-10-03":{"dateKind":"01","dateName":"\uac1c\ucc9c\uc808","isHoliday":"Y","locdate":20231003,"seq":1},"2023-10-09":{"dateKind":"01","dateName":"\ud55c\uae00\ub0a0","isHoliday":"Y","locdate":20231009,"seq":1},"2023-12-25":{"dateKind":"01","dateName":"\uae30\ub3c5\ud0c4\uc2e0\uc77c","isHoliday":"Y","locdate":20231225,"seq":1},"2024-01-01":{"dateKind":"01","dateName":"1\uc6d41\uc77c","isHoliday":"Y","locdate":20240101,"seq":1},"2024-02-09":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20240209,"seq":1},"2024-02-10":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20240210,"seq":1},"2024-02-11":{"dateKind":"01","dateName":"\uc124\ub0a0","isHoliday":"Y","locdate":20240211,"seq":1},"2024-02-12":{"dateKind":"01","dateName":"\ub300\uccb4\uacf5\ud734\uc77c(\uc124\ub0a0)","isHoliday":"Y","locdate":20240212,"seq":1},"2024-03-01":{"dateKind":"01","dateName":"\uc0bc\uc77c\uc808","isHoliday":"Y","locdate":20240301,"seq":1},"2024-04-10":{"dateKind":"01","dateName":"\uad6d\ud68c\uc758\uc6d0\uc120\uac70","isHoliday":"Y","locdate":20240410,"seq":1},"2024-05-05":{"dateKind":"01","dateName":"\uc5b4\ub9b0\uc774\ub0a0","isHoliday":"Y","locdate":20240505,"seq":1},"2024-05-06":{"dateKind":"01","dateName":"\ub300\uccb4\uacf5\ud734\uc77c(\uc5b4\ub9b0\uc774\ub0a0)","isHoliday":"Y","locdate":20240506,"seq":1},"2024-05-15":{"dateKind":"01","dateName":"\ubd80\ucc98\ub2d8\uc624\uc2e0\ub0a0","isHoliday":"Y","locdate":20240515,"seq":1},"2024-06-06":{"dateKind":"01","dateName":"\ud604\ucda9\uc77c","isHoliday":"Y","locdate":20240606,"seq":1},"2024-08-15":{"dateKind":"01","dateName":"\uad11\ubcf5\uc808","isHoliday":"Y","locdate":20240815,"seq":1},"2024-09-16":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20240916,"seq":1},"2024-09-17":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20240917,"seq":1},"2024-09-18":{"dateKind":"01","dateName":"\ucd94\uc11d","isHoliday":"Y","locdate":20240918,"seq":1},"2024-10-03":{"dateKind":"01","dateName":"\uac1c\ucc9c\uc808","isHoliday":"Y","locdate":20241003,"seq":1},"2024-10-09":{"dateKind":"01","dateName":"\ud55c\uae00\ub0a0","isHoliday":"Y","locdate":20241009,"seq":1},"2024-12-25":{"dateKind":"01","dateName":"\uae30\ub3c5\ud0c4\uc2e0\uc77c","isHoliday":"Y","locdate":20241225,"seq":1}}}';
echo '<pre>';
print_r(json_decode($d,true));
echo '</pre>';
결과는 내용이 조금 길어서 불필요할듯하여 생략하고 아래는 공공데이터포털에서 제공해주는 `한국천문연구원_특일 정보` API를 이용하여 JSON 데이터로 만드는 예제이니 참고하길 바란다.
<?php
// http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?solYear=2019&solMonth=03&ServiceKey=서비스키
$ard = array();
for($y=2023; $y <= 2024; $y++){
for($m = 1; $m <= 12; $m++){
$response = request_curl($y,sprintf("%02d",$m));
$d = json_decode($response['data'], true);
if( $d['response']['body']['totalCount'] == 1){
$item = $d['response']['body']['items']['item'];
$d['response']['body']['items']['item'] = array();
$d['response']['body']['items']['item'][0] = $item;
}
foreach($d['response']['body']['items']['item'] as $sk=>$sv){
$date = date('Y-m-d',strtotime($sv['locdate'].'000000'));
$ard['all'][] = $date;
$ard['detail'][$date] = $sv;
}
}
}
echo json_encode($ard);
function request_curl($solYear, $solMonth ){
$ServiceKey = ''; // 서비스키 입력
$data = array(
'solYear'=>$solYear, // 년도
'solMonth'=>$solMonth, // 월 -> 1~9월은 앞에 9을 붙여주어야함
'numOfRows'=>100, // 노출개수
'_type'=>'json', // 타입 xml 또는 json
'ServiceKey'=>$ServiceKey, // 서비스키
);
$url = 'http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?'.http_build_query($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
// 통신 실패 처리
if (curl_error($ch)){
return array('rst'=>'fail','msg'=>'CURL Error('.curl_errno( $ch ).') '.curl_error($ch));
}
// 통신에 대한 성공이며, 실제 데이터에 대한 성공은 별도 처리 필요
else{
return array('rst'=>'success','data'=>$data);
}
curl_close($ch);
}