<?php
function getProviderList()
{
    // Define the API endpoint and credentials
    $url = "https://api.nexusggr.com";
    $data = [
        "method" => "provider_list",
        "agent_code" => "anak_2",
        "agent_token" => "4bee33f2233e7e5db50618571cd6f29a"
    ];

    // Set up the POST request with cURL
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

    // Execute the request and capture the response
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

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

    // Handle the response based on status
    if ($httpCode == 200 && isset($result['status'])) {
        if ($result['status'] == 1) {
            // Success: Process provider list
            return $result['providers'];
        } else {
            // Failure: Log the error message
            return "Error: " . $result['msg'];
        }
    } else {
        // Connection or response error
        return "Failed to connect or invalid response";
    }
}

// Example usage
$providers = getProviderList();
if (is_array($providers)) {
    foreach ($providers as $provider) {
        echo "Provider Code: " . $provider['code'] . "\n";
        echo "<br>";
        echo "Provider Name: " . $provider['name'] . "\n";
        echo "<br>";
        echo "Status: " . ($provider['status'] ? "Open" : "Maintenance") . "\n\n";
        echo "<br>";
    }
} else {
    echo $providers; // Print error message
}
?>