<?php

function curlAddAlterStudentLedgersFinware($finalArray)
{
    try {
        // Initialize cURL session
        $ch = curl_init();

        // Get cURL URL and API signature information
        $curl_info = StudentAddAlterCurlUrl();
        $url = $curl_info['url'];
        $api_signature = $curl_info['api_signature'];

        // Prepare the payload to send
        $payload = json_encode(array("students" => $finalArray, "api_signature" => $api_signature));

        // Set cURL options
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Execute cURL request
        $result = curl_exec($ch);
        // print_R($result);die();
        // Check if there was an error during cURL execution
        if ($result === false) {
            $error_msg = curl_error($ch);
            curl_close($ch);  // Close cURL handle
            echo json_encode(array('status' => 0, 'error' => 'cURL error: ' . $error_msg));
            die();
        }

        // Close cURL handle after executing
        curl_close($ch);

        // Try to decode the JSON response
        $data = json_decode($result, true);
       
        if (isset($data['error']) && $data['error']) {
            errorreturn($data['message']);
            die();
        }

        // If no error, return the decoded data
        return $data;
    } catch (Exception $e) {
        // Catch and display any exception that occurs
        echo json_encode(array('status' => 0, 'error' => 'Exception: ' . $e->getMessage()));
        die();
    }
}

function StudentAddAlterCurlUrl()
{
    // Define your cURL URL and API signature here
    $url = $GLOBALS['curl_url_finware'] .'resources/external_requests/education_curl/curl_groups_ledgers/curl_add_alter_student_ledgers.php';
    return array('url' => $url, 'api_signature' => 'WR5yGWg6mZ');
}
