<?php
function get_cookietpo($challenge)
{
    $password="Cdr@us3r123";
    $token=MD5($challenge.$password);
    $requestArray = [
        "request" => [
            "action" => "login",
            "token" => $token,
            "user" => "cdruser"
        ]
    ];
    
    // To convert the array to JSON (if needed):
    $requestJson = json_encode($requestArray);
    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://41.221.51.92:8089/api',
    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 =>$requestJson,
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Cookie: HttpOnly; CookieName=CookieValue; TRACKID=eb08b509b72dbbb05fe02f0d98916950; session-identify=sid88710864-1722432494'
    ),
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_SSL_VERIFYPEER => 0,
    ));

    $response = curl_exec($curl);

    $data = json_decode($response, true);

    //echo "<br/>";
    //print_r($data);
    // Extract the challenge value
    $cookie = $data['response']['cookie'];

    curl_close($curl);

    return $cookie;
}

function  doenload_fileaudio($cookie,$filePath,$audiod)
{

    $headersArray = [
        'Content-Type: application/json',
        'Cookie: HttpOnly; CookieName=CookieValue; TRACKID=eb08b509b72dbbb05fe02f0d98916950; session-identify=' . $cookie
    ];


    $requestArray = [
        "request" => [
            "action" =>"recapi",
            "cookie" =>$cookie,
            "filename" =>$audiod
        ]
    ];
    
    // To convert the array to JSON (if needed):
    $requestJson = json_encode($requestArray);
    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://41.221.51.92:8089/api',
  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 =>$requestJson,
  CURLOPT_HTTPHEADER =>$headersArray,
  CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_SSL_VERIFYPEER => 0,
));

$response = curl_exec($curl);

if (file_put_contents($filePath, $response) === false) {
    throw new Exception('Failed to save file to ' . $filePath);
}

curl_close($curl);
//echo $response;


}