<?php
$client_id = "gxulTg1AQByUbeQH-nf77w-4yPaRusQUI_iQLizgvw";
$client_secret = "0UEhzxACAkQlB6r1YgSbDeVpP_mmwcWC46Q3jQEKBE4";

$url = "https://sandbox.kopokopo.com/oauth/token";

// Data body
$data = [
    "grant_type" => "client_credentials"
];

// Encode client_id:client_secret in base64
$credentials = base64_encode($client_id . ":" . $client_secret);

// Initialize cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Authorization: Basic " . $credentials
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

// Execute request
$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo "cURL error: " . curl_error($ch);
    exit;
}

curl_close($ch);

// Decode JSON response
$result = json_decode($response, true);

if (isset($result["access_token"])) {
    echo "✅ Access Token: " . $result["access_token"];
    echo "<br>⏳ Expires in: " . $result["expires_in"] . " seconds";
} else {
    echo "❌ Error: " . $response;
}
?>
