<?php
header("Authorization: KakaoAK xxxxxxxxxxxxxxxxxxxxxxxxx");
function getFromUrl($url, $method = 'GET')
{
$ch = curl_init();
// $agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)';
switch(strtoupper($method))
{
case 'GET':
curl_setopt($ch, CURLOPT_URL, $url);
break;
case 'POST':
$info = parse_url($url);
$url = $info['scheme'] . '://' . $info['host'] . $info['path'];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $info['query']);
break;
default:
return false;
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
// curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
$query = urlencode("전북 삼성동 100");
$res = getFromUrl('https://dapi.kakao.com/v2/local/search/address.json?$page=1&size=10&query='.$query, 'GET');
var_dump($res);
?>
@lyong0612
코드 첫번째 줄에 header 함수는 curl 호출 시 적용되지 않을 거에요.
아래 토픽을 살펴보시고, 힌트를 얻어 보세요. request 함수를 보시면 도움이 되실겁니다.
https://devtalk.kakao.com/t/php-rest-api/14602
아래 코드에서 $page가 오타인거 같은데 $가 없어야 할거 같고요.
$res = getFromUrl('https://dapi.kakao.com/v2/local/search/address.json?$page=1&size=10&query='.$query, 'GET');
호출 코드 참고 하세요.
<?php
function request($path, $query, $content_type = 'json')
{
$api_server = 'https://dapi.kakao.com';
$headers = array('Authorization: KakaoAK {YOUR_REST_API_KEY} ');
$opts = array(
CURLOPT_URL => $api_server . $path . '.' . $content_type . '?' . $query,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSLVERSION => 1,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => $headers
);
$curl_session = curl_init();
curl_setopt_array($curl_session, $opts);
$return_data = curl_exec($curl_session);
if (curl_errno($curl_session)) {
throw new Exception(curl_error($curl_session));
} else {
//print_r(curl_getinfo($curl_session));
curl_close($curl_session);
return $return_data;
}
}
$path = '/v2/local/search/address';
$content_type = 'json'; // json or xml
$params = http_build_query(array(
'page' => 1,
'size' => 10,
'query' => '전북 삼성동 100'
));
$res = request($path, $params, $content_type);
header('Content-Type: application/' . $content_type . '; charset=utf-8');
echo $res;
1개의 좋아요
감사합니다.