<?php

function api_plaque_1($immat)
{
  try {
    $curl = curl_init();

    $raw_immat = strtolower($immat);
    $immat = str_replace('-', '', $immat);
    $immat = str_replace('-', '', $immat);
    $immat = trim($immat);

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://www.smeno.com/assurances/assurance-auto/devis/?wazari=1&endpoint=Vehicle/immatriculation/$immat",
      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(
        'Accept: application/json, text/javascript, */*; q=0.01',
        'Accept-Language: en-US,en;q=0.9',
        'Connection: keep-alive',
        'Cookie: fe_typo_user=38e81f56b908f75e47bc6e1a7120d4f6; _gcl_aw=GCL.1656816214.CjwKCAjw2f-VBhAsEiwAO4lNePDMHHTBFD_HVatGFMbwmNe9UbjwiPJRPgAbhdLwA6BFjpt8pLhtCxoCvIQQAvD_BwE; _gcl_au=1.1.2060125041.1656816214; wz_ht=1; axeptio_cookies={%22$$token%22:%2246e6atrxk7hoi6jf85f489%22%2C%22$$date%22:%222022-07-03T02:47:49.066Z%22%2C%22google_analytics%22:true%2C%22facebook_pixel%22:true%2C%22Google_Ads%22:true%2C%22$$completed%22:true}; axeptio_authorized_vendors=%2Cgoogle_analytics%2Cfacebook_pixel%2CGoogle_Ads%2C; axeptio_all_vendors=%2Cgoogle_analytics%2Cfacebook_pixel%2CGoogle_Ads%2C; _gid=GA1.2.187406942.1656816469; _gac_UA-27236420-6=1.1656816513.CjwKCAjw2f-VBhAsEiwAO4lNePDMHHTBFD_HVatGFMbwmNe9UbjwiPJRPgAbhdLwA6BFjpt8pLhtCxoCvIQQAvD_BwE; _ga_S606TZBWD2=GS1.1.1656816215.1.1.1656816558.0; _ga=GA1.2.914824890.1656816216; fe_typo_user=aa8034fbc7df59646adf5b7b032aedb2',
        'Referer: https://www.smeno.com/assurances/assurance-auto/devis/',
        'Sec-Fetch-Dest: empty',
        'Sec-Fetch-Mode: cors',
        'Sec-Fetch-Site: same-origin',
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        'X-Requested-With: XMLHttpRequest',
        'sec-ch-ua: ".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
        'sec-ch-ua-mobile: ?0',
        'sec-ch-ua-platform: "macOS"'
      ),
    ));

    $response = curl_exec($curl);

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

    $data = [
      'Marque' => '',
      'ModeleEtude' => '',
      'PuissFisc' => '',
      'CO2' => '',
      'Energie' => '',
      'GenreCG' => '',
      'DateMec' => '',
      'Immatriculation' => '',
    ];

    // return $response;
    if ($response) {
      $response = json_decode($response, true);
      if (isset($response['marque'])) {
        $data['Marque'] = $response['marque'];
        $data['ModeleEtude'] = $response['modele'];
        $data['PuissFisc'] = $response['chevauxFiscaux'];
        // $data['Energie'] = $response['energie'];
        $data['GenreCG'] = $response['carrosserie'];
        $data['DateMec'] = $response['dateMiseEnCirculation'];
        if (isset($response['dateMiseEnCirculation'])) {
          $data['DateMec'] = str_replace('T00:00:00', '', $data['DateMec']);
        }
      }

      $immat = strtoupper($immat);
      $data['Immatriculation'] = $immat;

      return $data;
    }
  } catch (Throwable $e) {
    return false;
    // echo $e->getMessage();
  }
}

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