<?php

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://cxone.niceincontact.com/auth/token',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => 'grant_type=password&username=JKU4CU462VPSRP43RRXVGKH5HRP2FBZT444IZNAMKYBDN7LRIGJQ%3D%3D%3D%3D&password=NBHCQYDOW4IWGY3IVFGSJRI2KSBPX6UD44IXVYCV5OHIQSFFPOVA%3D%3D%3D%3D&client_id=b4d5a185-da99-4898-a733-4c1d678dcaab&client_secret=yckO9VQg6b3JQd%2Fg%2FXwudQ%3D%3D',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/x-www-form-urlencoded'
    ),
));

$response = curl_exec($curl);
curl_close($curl);
$accessToken = json_decode($response, true)['access_token'];
echo "<code>Access Token:<br>" . $accessToken . "</code> ";
// echo $accessToken;


$curl = curl_init();
////// https://developer.niceincontact.com/API/RealTimeDataAPI#/RealTime/SkillActivity

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api-c63.nice-incontact.com/incontactapi/services/v32.0/skills/activity?IsOutbound=false&fields=skillname%2CagentsAvailable%2CagentsIdle&skip=55&top=1',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
        ////// New 28 July 25
        'Authorization: Bearer ' . $accessToken,
        'Cookie: AWSALB=aRHyJlUCTGUpZHHk3KOdZc2HlNI9aSmeJc8qQZyTSQjO0JjhuMZxkXkeojG36lexp72OeUsurH5juCYElJp+Sb/8qJOvD97cQN8EJGCYQD50xRiJAxG8dhlRkCI7; AWSALBCORS=aRHyJlUCTGUpZHHk3KOdZc2HlNI9aSmeJc8qQZyTSQjO0JjhuMZxkXkeojG36lexp72OeUsurH5juCYElJp+Sb/8qJOvD97cQN8EJGCYQD50xRiJAxG8dhlRkCI7'
    ),
));

$response = curl_exec($curl);

curl_close($curl);
$jsonRes = json_decode($response, true);
echo "<hr>Response: <br><pre>";
print_r($jsonRes);

$available = ((isset($jsonRes['skillActivity'][0]['agentsAvailable'])) ? $jsonRes['skillActivity'][0]['agentsAvailable'] : 0);
$total_agents = ((isset($jsonRes['skillActivity'][0]['agentsIdle'])) ? $jsonRes['skillActivity'][0]['agentsIdle'] : 0);
if ($available >= 1) {
    if ($total_agents > 1) {
        $msg = $total_agents . " Agents are available";
    } else if ($total_agents == 1) {
        $msg = $total_agents . " Agent is available";
    } else {
        $msg = "No Agents are available";
    }
} else {
    $msg = "No Agents are available";
}

echo "<hr>Final Response: " . $msg;
