<?php

class Cgi
{

    static $service_uri = "https://eservices.cgi.ci/backavit/avit";
    static $token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJmcmFuay52YXNxdWV6IiwianRpIjoiMSIsImV4cCI6MTc3OTEyMzA2Nn0.A0e05bhJY2phvdHQauFi-XSthTE3Ekyqm-0IV3rD9F2whEysut-N1_ehnedK87uDcSrLooWKFhiVL3qPEjuDzMCSaBt087p4uNWjEFl2g9NqjVzK_0g6zct63P2ztlMyutuT7OUNEXDm9rKcXMcL_gt-Yvf9--7DqBwdPgRlyOCJEOFkQRvcS9wC2nQFcIPU7cuD0wjDUDfEcpMOGyUOFntnbsF53os2oKJO2t5d1DTBzbYkUMHUK2pSkQo0cMJSOYbXAsUFWT06tooRvAzz5otVkKJnOT7KkhcpcpnjG5tCCeMKJ9oc9Fgkc0SE4r-E5xQiELDG4E45f3zQjcuBcg";

    static function curl_postData($post_arr, $headers_arr,  $route, $code_return = 0)
    {
        $url = self::$service_uri . $route;
        // Data to be sent in the POST request
        $data = json_encode($post_arr);

        // Initialize cURL session
        $ch = curl_init();

        // Set cURL options
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_arr);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

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

        // Check for errors
        if (curl_errno($ch)) {
            return false;
        } else {
            if ($code_return == 1) {
                $return_array = array(
                    "code_web" => null,
                    "message" => $response
                );
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                return $return_array;
            } else {
                return $response;
            }
        }

        // Close cURL session
        curl_close($ch);
    }

    static function curl_getData($headers_arr,  $route, $code_return = 0)
    {

        $url = self::$service_uri . $route;

        // Initialize cURL session
        $ch = curl_init();

        // Set cURL options
        curl_setopt($ch, CURLOPT_URL, $url); // URL cible
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_arr);

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

        // Check for errors
        if (curl_errno($ch)) {
            return false;
        } else {
            if ($code_return == 1) {
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                $return_array = array(
                    "code_web" => $http_code,
                    "message" => $response
                );
                return $return_array;
            } else {
                return $response;
            }
        }

        // Close cURL session
        curl_close($ch);
    }
}
