나. vworld_function.php
(1) 사용자 정의 함수 구문
<?php function getAddr($coord, $vworldKey) { 처리 구문 } ?> |
php의 함수도 일반적인 함수 구문과 동일합니다.
function 다음에 함수명을 적고, 괄호 안에 전달한 인수를 적습니다.
(2) getAddr 함수의 내용
(가) url과 전달할 인수 만들기
$ch = curl_init(); $url = 'https://api.vworld.kr/req/address'; // URL $queryParams = '?' . 'key=' . $vworldKey; // API Key $queryParams .= '&service=' . "address"; $queryParams .= '&format=' . "json"; $queryParams .= '&request=' . "getaddress"; $queryParams .= '&version=' . "2.0"; $queryParams .= '&crs=' . "EPSG:4326"; $queryParams .= '&type=' . "BOTH"; $queryParams .= '&point=' . $coord; |
curl을 이용합니다.
시작은 curl_init이고, 끝내는 것은 curl_close입니다.
curl에 전달할 url 주소와 인수를 키=변수로 정의해서 .(마침표)로 연결합니다.
echo $url . $queryParams;
로 url과 인수를 찍어보면
입니다.
(나) curl 옵션 설정 및 실행하기
curl_setopt($ch, CURLOPT_URL, $url . $queryParams); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); $response = curl_exec($ch); curl_close($ch); |
curl 옵션을 설정하고, curl_exec로 실행한 다음 결과를 $response로 받습니다.
그리고, curl을 닫습니다(close).
(다) JSON 처리하기
// JSON을 PHP 배열로 변환 $data = json_decode($response, true); $results = $data['response']['result']; $count = count($results); // 지번 주소와 도로명 주소를 따로 저장, detail은 도로명 주소에만 있음 foreach ($results as $result) { if ($result['type'] == 'parcel') { $jibun_addr = $result['text']; } else { $road_addr = $result['text']; $donghosu = $result['structure']['detail']; } } $zipcode = $data['response']['result'][0]['zipcode']; // 우편번호 $regionCd = $data['response']['result'][0]['structure']['level4LC']; |
$data = json_decode($response, true);
: JSON을 파싱 해서 $data에 저장합니다.
JSON 파일의 구조는 아래와 같습니다.
{ "response": { "service": { "name": "address", "version": "2.0", "operation": "getaddress", "time": "9(ms)" }, "status": "OK", "input": { "point": { "x": "126.93031717597813", "y": "37.52338067416902" }, "crs": "EPSG:4326", "type": "BOTH" }, "result": [ { "zipcode": "07339", "type": "parcel", "text": "서울특별시 영등포구 여의도동 32", "structure": { "level0": "대한민국", "level1": "서울특별시", "level2": "영등포구", "level3": "", "level4L": "여의도동", "level4LC": "1156011000", "level4A": "여의동", "level4AC": "1156054000", "level5": "32", "detail": "" } }, { "zipcode": "07339", "type": "road", "text": "서울특별시 영등포구 국제금융로7길 1 (여의도동)", "structure": { "level0": "대한민국", "level1": "서울특별시", "level2": "영등포구", "level3": "여의도동", "level4L": "국제금융로7길", "level4LC": "4154066", "level4A": "여의동", "level4AC": "1156054000", "level5": "1", "detail": "C동" } } ] } } |
(라) 결괏값 배열로 반환하기
// 배열로 반환 return [$jibun_addr, $road_addr, $donghosu, $zipcode, $regionCd]; |
return 다음에 배열을 나타내는 대괄호([])안에 값을 넣습니다.
(3) getLandPrice 함수의 내용
(가) 올해 구하기
$year = date("Y"); |
date("Y") 함수를 이용해 올해의 연도를 구할 수 있습니다.
(나) 개별공시지가 조회 연도 구하기
개별공시지가는 6월 말까지 고시되기 때문에 날짜에 따라 올해의 개별공시지가가 있을 수도 있고, 없을 수도 있습니다. 따라서, 올해 분이 없을 때는 -1을 해서 작년도분을 검색해야 합니다.
개별공시지가가 없을 때는 응답이 있을 경우와 달리
response 아래 totoalCount의 값이 0입니다.
따라서, 이걸 이용해서 $count 값을 구한 후 '0'(문자 0)인 경우에는 True이므로
연도($year)에서 1을 빼서 stdrYear(기준연도)를 $year로 바꿔주는 작업을 반복하고
$count가 0이 아니면 while문을 종료(break) 합니다.
$ch = curl_init(); $url = 'https://api.vworld.kr/ned/data/getIndvdLandPriceAttr'; // URL $queryParams = '?' . 'key=' . $vworldKey; $queryParams .= '&domain=' . $domain; $queryParams .= '&pnu=' . $pnu; $queryParams .= '&format=' . 'json'; $year_omit_url = $url.$queryParams; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // 개별공시지가 건수가 0일 때 반복 while (true and $year>2022) { // 연도를 하나 줄인 값으로 대체 $full_url = $year_omit_url.'&stdrYear=' . $year; curl_setopt($ch, CURLOPT_URL, $full_url); $response = curl_exec($ch); // JSON을 PHP 배열로 변환 $data = json_decode($response, true); $count = $data['response']['totalCount']; if ($count == '0') { $year--; } else { break; } } curl_close($ch); |
$year_omit_url = $url.$queryParams;
$full_url = $year_omit_url.'&stdrYear=' . $year;
기준연도를 입력하는 부분은
먼저 기준 연도를 입력하는 부분을 제외한 $year_omit_url을 만든 다음
기준연도를 추가하도록 했습니다.
그리고, curl 부분도 반복을 줄이기 위해 $full_url을 넣는 부분만 빼고
나머지는 while 문 앞에 뒀습니다.
4. 확장 가능성
이렇게 해서 개별공시지가를 구하는 것을 해봤습니다.
이것을 이용하면 토지특성속성, 토지이용계획속성, 공동주택가격속성 등 vworld에서 제공하는 다양한 정보를 조회해서 지도에 표시할 수 있습니다.
'Map(WEB)' 카테고리의 다른 글
2025년 상반기 V-World 온라인 교육 일정 안내 (0) | 2025.04.17 |
---|---|
VWorld의 WMS, WFS 기능 구현 (0) | 2025.03.27 |
vworld 지도 만들기(16) - 개별공시지가 표시하기(4) : PHP(2) (0) | 2025.03.06 |
vworld 지도 만들기(15) - 개별공시지가 표시하기(3) : PHP(1) (0) | 2025.03.05 |
vworld 지도 만들기(14) - 개별공시지가 표시하기(2) : JSONP (0) | 2025.03.04 |