OPENDART api를 이용한 공시정보 가져오기

OPENDART의 api를 이용해 공시정보를 가져오는 코드를 작성했다. api 사용을 위해서는 먼저 https://opendart.fss.or.kr/ 에서 회원가입 후 인증키를 발급받아야 한다. 또한 공시대상회사의 고유번호를 알아야 한다. 공시정보와 고유번호 확인 가이드 문서는 아래의 링크를 방문하면 된다.
- 공시정보 : https://opendart.fss.or.kr/guide/detail.do?apiGrpCd=DS001&apiId=2019001
- 고유번호 : https://opendart.fss.or.kr/guide/detail.do?apiGrpCd=DS001&apiId=2019018
PHP로 작성된 코드는 아래와 같다.
<?php
function getDisclosureInfo($page = 1, $rows = 10)
{
$parms = array(
'crtfc_key' => '',
'corp_code' => '',
'bgn_de' => '20201101',
'end_de' => date(Ymd, time()),
'page_no' => $page,
'page_count' => $rows
);
$headers = [
'Accept: ' . $_SERVER['HTTP_ACCEPT'],
'Accept-Encoding: gzip, deflate',
'Accept-Language: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE'],
'Cache-Control: no-cache',
'User-Agent: ' . $_SERVER['HTTP_USER_AGENT']
];
$url = 'https://opendart.fss.or.kr/api/list.json?'.http_build_query($parms);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
if (curl_errno($ch))
return 'Curl error: '.curl_errno($ch);
curl_close($ch);
$return = trim($return);
$json = json_decode($return);
if (!$json)
return 'JSON error';
if ($json->status != '000')
return 'Error : '.$json->message . '(' . $json->status . ')';
$data = array();
$list = array();
$data['total'] = $json->total_count;
$data['pages'] = $json->total_page;
foreach ($json->list as $v) {
$info = array();
$info['no'] = $v->rcept_no;
$info['title'] = $v->report_nm;
$info['date'] = preg_replace('#^([\d]{4})([\d]{2})([\d]{2})$#', '\\1-\\2-\\3', $v->rcept_dt);
$info['submit'] = $v->flr_nm;
$info['href'] = 'http://dart.fss.or.kr/dsaf001/main.do?rcpNo='.$v->rcept_no;
$info['mhref'] = 'http://m.dart.fss.or.kr/html_mdart/MD1007.html?rcpNo='.$v->rcept_no;
$list[] = $info;
}
$data['list'] = $list;
return $data;
}
요청 파라미터 중 bgn_de
의 값은 최소 회사의 상장일 이전으로 설정해야 할 것으로 생각된다. 위 코드로 얻어진 결과는 아래와 같다.
Array
(
[total] => 12
[pages] => 2
[list] => Array
(
[0] => Array
(
[no] => 20201216900543
[title] => 주식명의개서정지(주주명부폐쇄)
[date] => 2020-12-16
[submit] => 앱코
[href] => http://dart.fss.or.kr/dsaf001/main.do?rcpNo=20201216900543
[mhref] => http://m.dart.fss.or.kr/html_mdart/MD1007.html?rcpNo=20201216900543
)
[1] => Array
(
[no] => 20201207000152
[title] => 주식등의대량보유상황보고서(일반)
[date] => 2020-12-07
[submit] => 오광근
[href] => http://dart.fss.or.kr/dsaf001/main.do?rcpNo=20201207000152
[mhref] => http://m.dart.fss.or.kr/html_mdart/MD1007.html?rcpNo=20201207000152
)