<?php
require_once __DIR__ . '/../config/config.php';
$token = getConfigFromDB('whatsapp_token', '');
$phoneId = getConfigFromDB('whatsapp_phone_number_id', '');
$maskedToken = $token ? substr($token,0,6).'...'.substr($token,-4) : '(empty)';
echo "Phone ID: $phoneId\nToken: $maskedToken\n\n";
$url = "https://graph.facebook.com/v22.0/{$phoneId}?access_token=".urlencode($token);
$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 15,
    CURLOPT_SSL_VERIFYPEER => true
]);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
    echo "cURL error: $err\n";
    exit(1);
}
echo "HTTP Code: $code\n";
echo "Response:\n";
echo $response . "\n";
$decoded = json_decode($response, true);
if ($decoded && isset($decoded['error'])) {
    echo "\nDetected error message: " . ($decoded['error']['message'] ?? json_encode($decoded['error'])) . "\n";
}
