[PHP] 카카오 로그인 API

// 로그인 페이지 링크
https://kauth.kakao.com/oauth/authorize?client_id=[API 키]&redirect_uri=[로그인 처리 페이지 URL]&response_type=code

// 로그인 처리 페이지
$returnCode = $_GET["code"];
$restAPIKey = "[API 키]";
$callbacURI = urlencode("[로그인 처리 페이지 URL]");

$returnUrl = "https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id=" . $restAPIKey . "&redirect_uri=" . $callbacURI . "&code=" . $returnCode;

$isPost = false;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $returnUrl);
curl_setopt($ch, CURLOPT_POST, $isPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CAINFO, "../inc/cacert.pem");

$headers = array();
$loginResponse = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "<pre>",var_dump($loginResponse),"</pre>";
$accessToken = json_decode($loginResponse)->access_token;

// 사용자 정보 획득
$returnUrl = "https://kapi.kakao.com/v2/user/me";

$isPost = false;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $returnUrl);
curl_setopt($ch, CURLOPT_POST, $isPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$accessToken, 'Content-Type: application/x-www-form-urlencoded;charset=utf-8'));
curl_setopt($ch, CURLOPT_CAINFO, "../inc/cacert.pem");

$headers = array();
$loginResponse = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "<pre>",var_dump($loginResponse),"</pre>";

Leave a Comment