Alimtalk.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Controllers;
  3. use CodeIgniter\Controller;
  4. class Alimtalk extends Controller
  5. {
  6. public function send()
  7. {
  8. $receivers = [
  9. [
  10. 'phone' => '01011112222',
  11. 'name' => '홍길동',
  12. // 아래 부분만 알림톡 템플릿 변수와 1:1 매칭!
  13. '이벤트 명' => '여름휴가 응모 이벤트',
  14. '상품명' => '스타벅스 기프티콘',
  15. '당첨일' => '2024-06-12',
  16. '고객센터번호' => '1544-1234'
  17. ]
  18. ];
  19. // 기본 파라미터
  20. $variables = [
  21. 'apikey' => 'npb9ryvqh9439js2sfbhuji4lwfmdgqu',
  22. 'userid' => 'interscope',
  23. 'senderkey' => '846700656ab2c0b136e4433751d75018ea48b8ec',
  24. 'tpl_code' => 'UA_1201',
  25. 'sender' => '010-8384-5309',
  26. //'senddate' => date("YmdHis", strtotime("+10 minutes")),
  27. ];
  28. // 수신자별 파라미터 추가
  29. foreach ($receivers as $idx => $info) {
  30. $i = $idx + 1;
  31. $variables["receiver_{$i}"] = $info['phone'];
  32. $variables["recvname_{$i}"] = $info['name'];
  33. // 템플릿 변수값 매칭
  34. $variables["subject_{$i}"] = ''; // 필요 없으면 공란
  35. $variables["message_{$i}"] =
  36. "안녕하세요 고객님!\n"
  37. . "{$info['이벤트 명']}\n"
  38. . "{$info['상품명']} 당첨을 축하드립니다.\n"
  39. . "당첨일자 : {$info['당첨일']}\n\n"
  40. . "*이 메시지는 고객님이 참여한 이벤트 당첨으로 발송된 안내 메시지입니다.\n\n"
  41. . "문의 : 고객센터 {$info['고객센터번호']}\n"
  42. . "감사합니다.";
  43. // 버튼 필요시 추가
  44. // $variables["button_{$i}"] = '...';
  45. }
  46. $apiURL = 'https://kakaoapi.aligo.in/akv10/alimtalk/send/';
  47. $hostInfo = parse_url($apiURL);
  48. $port = (strtolower($hostInfo['scheme']) == 'https') ? 443 : 80;
  49. // cURL 요청
  50. $ch = curl_init();
  51. curl_setopt($ch, CURLOPT_PORT, $port);
  52. curl_setopt($ch, CURLOPT_URL, $apiURL);
  53. curl_setopt($ch, CURLOPT_POST, 1);
  54. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  55. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  57. $response = curl_exec($ch);
  58. $error_msg = curl_error($ch);
  59. curl_close($ch);
  60. // 결과 출력 (json)
  61. if ($error_msg) {
  62. return $this->response->setJSON(['error' => $error_msg]);
  63. }
  64. $result = json_decode($response, true);
  65. return $this->response->setJSON($result);
  66. }
  67. }