<?php
function api_plaque_2($immat)
{
  try {
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://www.carte-grise.org/js/jQuery/getDonneesImmatriculation.php?immatriculation=$immat&_=1637885278525",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => false,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'Connection: keep-alive',
        'Pragma: no-cache',
        'Cache-Control: no-cache',
        'sec-ch-ua: "Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
        'Accept: text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
        'X-Requested-With: XMLHttpRequest',
        'sec-ch-ua-mobile: ?0',
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
        'sec-ch-ua-platform: "macOS"',
        'Sec-Fetch-Site: same-origin',
        'Sec-Fetch-Mode: cors',
        'Sec-Fetch-Dest: empty',
        'Referer: https://www.carte-grise.org/',
        'Accept-Language: en-US,en;q=0.9',
        'Cookie: __utma=218644107.1519950547.1616103954.1618175103.1621020684.7; axeptio_authorized_vendors=%2C%2C; axeptio_all_vendors=%2C%2C; _ga=GA1.2.1519950547.1616103954; _hjid=b19d0c44-a29a-4492-a4c0-cc7ea1e80251; axeptio_cookies={%22$$token%22:%22e3ncjyxc868xjzjfr2cjka%22%2C%22$$date%22:%222021-09-30T19:14:50.714Z%22%2C%22$$completed%22:true}; referer=service-cartegrise.fr; CGOSESS=j5rng8789g3i54ca0m2pv346q1; _gid=GA1.2.1546715417.1637885260; _gat_UA-670854-2=1; referer=service-cartegrise.fr'
      ),
    ));

    $response = curl_exec($curl);
    // var_dump($response);
    // die();

    curl_close($curl);

    $data = [
      'Immatriculation' => $immat,
      'Marque' => '',
      'ModeleEtude' => '',
      'PuissFisc' => '',
      'CO2' => '',
      'Energie' => '',
      'GenreCG' => '',
      'DateMec' => '',
    ];
    if ($response) {
      $response = json_decode($response, true);
      if (isset($response['immat'])) {
        $data['Marque'] = $response['marque'];
        $data['ModeleEtude'] = $response['modele'];
        $data['PuissFisc'] = $response['puisFisc'];
        $data['CO2'] = $response['co2'];
        $data['Energie'] = $response['energieNGC'];
        $data['GenreCG'] = $response['genreVCGNGC'];
        $data['DateMec'] = $response['date1erCir_fr'];
        return $data;
      } else {
        return false;
      }
    } else {
      return false;
    }


    return $data;
  } catch (\Throwable $th) {

    return false;
  }
}




// $result  = api_plaque_2('AA123BC');
// $result = json_encode($result);
// echo $result;
