prepare("SELECT * FROM whatsapp_cloud_api GROUP BY id_data_server"); $stmt->execute(); $whatsapp = $stmt->fetchAll(PDO::FETCH_ASSOC); $adaPesan = false; foreach ($whatsapp as $waKey) { $stmt = $pdo->prepare(" SELECT id, pesan FROM master_pesan WHERE id_data_server = :id_data_server AND status = 'PENDING' AND gateway_type = 'official' LIMIT 1 "); $stmt->execute([':id_data_server' => $waKey['id_data_server']]); $pesan = $stmt->fetch(PDO::FETCH_ASSOC); if ($pesan) { $adaPesan = true; $access_token = $waKey['token_access']; $phone_number_id = $waKey['number_id']; $url = "https://graph.facebook.com/v17.0/$phone_number_id/messages"; $data = json_decode($pesan['pesan'], true); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $access_token", "Content-Type: application/json" ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $response = curl_exec($ch); if (curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } else { $responseData = json_decode($response, true); // decode JSON if (isset($responseData['messages'][0]['id'])) { $wa_message_id = $responseData['messages'][0]['id']; $stmt = $pdo->prepare("UPDATE master_pesan SET status = 'PROSES', wa_message_id = :wa_id WHERE id = :id"); $stmt->execute([':wa_id' => $wa_message_id, ':id' => $pesan['id']]); echo "Pesan ID {$pesan['id']} Cloud API Official berhasil dikirim, WA ID: $wa_message_id
"; } else { echo "Gagal mengirim pesan Cloud API Official ID {$pesan['id']}, response: $response
"; } } curl_close($ch); } } // if (!$adaPesan) { // echo "Tidak ada pesan perlu dikirim Cloud API Official"; // }