$client_id = 'a0914fd445ed6c2*****************';
$client_secret = 'lHLzfDnbs61ufqmoH6T0U4IMDSD4i5pg';
$redirect_uri = 'https://kakaonara.co.kr/kakao_callback.php';
if (isset($_GET['code'])) {
$code = $_GET['code'];
$token_url = 'https://kauth.kakao.com/oauth/token';
$token_params = [
'grant_type' => 'authorization_code',
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'code' => $code,
];
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$token_response = curl_exec($ch);
// cURL 오류 정보 출력
if (curl_errno($ch)) {
echo 'cURL 오류: ' . curl_error($ch);
}
curl_close($ch);
$token_data = json_decode($token_response, true);
if (isset($token_data['access_token'])) {
// 친구 목록 가져오기
$api_url = 'https://kapi.kakao.com/v1/api/talk/friends';
$headers = [
'Authorization: Bearer ' . $token_data['access_token'],
];
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$friends_response = curl_exec($ch);
curl_close($ch);
$friends_data = json_decode($friends_response, true);
$friend_count = count($friends_data['elements']);
echo "친구 수: {$friend_count}";
if (isset($friends_data['elements'])) {
// 친구에게 메시지 보내기
$api_url = 'https://kapi.kakao.com/v1/api/talk/friends/message/default/send';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$params = [
'receiver_uuids' => json_encode([$friends_data['elements'][0]['uuid']]), // 첫 번째 친구에게 메시지 보냄
'template_object' => json_encode([
'object_type' => 'text',
'text' => 'test massage',
'link' => [
'web_url' => 'https://kakaonara.co.kr',
'mobile_web_url' => 'https://kakaonara.co.kr'
]
])
];
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response_data = json_decode($response, true);
if (isset($response_data['result_code']) && $response_data['result_code'] == 0) {
echo '메시지 전송 성공';
} else {
echo '메시지 전송 실패';
}
}
}
}
친구목록이 1명만 나오는데 뭐가 잘못되었지 모르겠어요
TEST입니다.
ID 1010838