class Talk {
private $apiBaseUrl;
private $apiUrl;
private $cliendId;
private $clientSecret;
private $senderKey;
private $headers;
private $body;
private $token;
private $data;
private $messageType = 1;
private $version = 1;
public function __construct(){
$this->apiBaseUrl = "https://bizmsg-web.kakaoenterprise.com";
$this->cliendId = "cliendId";
$this->clientSecret = "clientSecret";
$this->senderKey = "senderKey";
$this->getToken();
}
private function curl(){
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_PORT, 443);
curl_setopt($oCurl, CURLOPT_URL, $this->apiUrl);
curl_setopt($oCurl, CURLOPT_POST, 1);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
if($this->body){
$this->body = $this->raw_json_encode($this->body);
//print_r($this->body);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $this->body);
}
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($oCurl);
curl_close($oCurl);
return json_decode($result);
}
private function getToken(){
$this->apiUrl = $this->apiBaseUrl . "/v2/oauth/token";
$this->headers = array(
'Authorization: Basic '.$this->cliendId.' '.$this->clientSecret,
'Content-Type: application/x-www-form-urlencoded',
);
$result = $this->curl();
$this->token = $result->access_token;
}
private function result(){
$this->apiUrl = $this->apiBaseUrl . "/v2/info/message/results";
$this->headers = array(
'Authorization: Bearer '.$this->token,
'Content-Type: application/json',
);
$this->body = array(
'start_date' => '2022-01-01',
'end_date' => '2022-12-31',
);
return $result = $this->curl();
}
public function send($reciver="",$talkData){
if($this->version == 1){
$this->apiUrl = $this->apiBaseUrl . "/v1/message/send";
}else{
$this->apiUrl = $this->apiBaseUrl . "/v2/send/kakao";
}
$this->headers = array(
'Authorization: Bearer '.$this->token,
'Content-Type: application/json',
);
switch($this->messageType){
case 1 :
$template_code = "sendPayment_v3";
$message = '테스트: '.$talkData['testData1'].PHP_EOL;
$message .= '테스트: '.$talkData['testData2'].PHP_EOL;
break;
}
$this->body = array(
'client_id' => $this->cliendId,
'cid' => 1,
'message_type' => 'AT',
'sender_key' => $this->senderKey,
'phone_number' => $reciver,
'template_code' => $template_code,
'message' => $message,
'sender_no' => 'sender_no',
'fall_back_yn' => true,
'fall_back_message_type' => 'LM',
);
return $result = $this->curl();
}
public function Talk($reciver, $talkData, $messageType = 1){
$this->messageType = $messageType;
$result = $this->send($reciver,$talkData);
}
private function raw_json_encode($input) {
return preg_replace_callback(
'/\\\\u([0-9a-zA-Z]{4})/',
function ($matches) {
return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
},
json_encode($input)
);
}
}