94 lines
3.2 KiB
PHP
94 lines
3.2 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
/**
|
|
* ============================================================
|
|
* Meta API Helper (Graph API v20)
|
|
* ------------------------------------------------------------
|
|
* Helper untuk mengirim request ke Meta (WhatsApp Business API)
|
|
* ============================================================
|
|
*/
|
|
|
|
if (!function_exists('meta_api_request')) {
|
|
/**
|
|
* Kirim request ke Meta API (GET / POST / PATCH / DELETE)
|
|
*
|
|
* @param string $endpoint contoh: '1234567890/messages'
|
|
* @param string $method 'GET'|'POST'|'PATCH'|'DELETE'
|
|
* @param array $params optional (body / query)
|
|
* @param string|null $access_token optional override token
|
|
* @return array|null hasil JSON decode
|
|
*/
|
|
function meta_api_request($endpoint, $method = 'GET', $params = [], $access_token = null)
|
|
{
|
|
$CI =& get_instance();
|
|
$CI->config->load('config');
|
|
|
|
// ✅ Gunakan Graph API terbaru (v20)
|
|
$base_url = 'https://graph.facebook.com/v20.0/';
|
|
$access_token = $access_token ?? $CI->config->item('meta_access_token');
|
|
|
|
if (!$access_token) {
|
|
log_message('error', 'Meta API: Access token belum diset di config.php');
|
|
return ['error' => ['message' => 'Access token not set']];
|
|
}
|
|
|
|
// Pastikan endpoint tidak dobel slash
|
|
$url = rtrim($base_url, '/') . '/' . ltrim($endpoint, '/');
|
|
|
|
$headers = [
|
|
'Authorization: Bearer ' . $access_token,
|
|
'Content-Type: application/json'
|
|
];
|
|
|
|
$ch = curl_init();
|
|
|
|
// ✅ Default method
|
|
$method = strtoupper($method);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
|
|
// ✅ Setup body/query sesuai metode
|
|
if (in_array($method, ['POST', 'PATCH'])) {
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
|
} elseif ($method === 'GET' && !empty($params)) {
|
|
$url .= '?' . http_build_query($params);
|
|
} elseif ($method === 'DELETE' && !empty($params)) {
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
|
|
}
|
|
|
|
// ✅ Set CURL options
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_HTTPHEADER => $headers,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_SSL_VERIFYPEER => true,
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$curl_error = curl_error($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
// ⚠️ Tangani error CURL
|
|
if ($curl_error) {
|
|
log_message('error', 'Meta API CURL error: ' . $curl_error);
|
|
return ['error' => ['message' => 'CURL error: ' . $curl_error]];
|
|
}
|
|
|
|
$data = json_decode($response, true);
|
|
|
|
// ⚠️ Tangani error HTTP
|
|
if ($http_code >= 400) {
|
|
log_message('error', sprintf(
|
|
'Meta API HTTP %s: %s',
|
|
$http_code,
|
|
$response
|
|
));
|
|
}
|
|
|
|
// ✅ Return hasil JSON
|
|
return $data ?: ['error' => ['message' => 'Invalid JSON response']];
|
|
}
|
|
}
|