<?php
/*$auth_data = array(
'client_id' => 'z8r6KvgvPRGSwzHWQCdMGL2Z4CEk7HG7',
'client_secret' => 'wcIX72i08MwboHN1',
'grant_type' => 'client_credentials'
);*/
error_reporting(0);

require_once 'vendor/rmccue/requests/library/Requests.php';
Requests::register_autoloader();
$url = 'https://test.api.amadeus.com/v1/security/oauth2/token';
$auth_data = array(
'client_id' => 'KEUu3vZkAEYsCq8snJUzcUl3GPkThNrM',
'client_secret' => 'b4G93MTdvTF4Pot3',
'grant_type' => 'client_credentials'
);
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');

$requests_response = Requests::post($url, $headers, $auth_data);
$response_body = json_decode($requests_response->body, true);
echo '<pre>';
$token = $response_body['access_token'];
$access_token = $token;

if (isset($_POST['query'])) {
$inpText = $_POST['query'];
$curl = curl_init();
$tkn = "$access_token";
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://test.api.amadeus.com/v1/reference-data/locations?subType=CITY,AIRPORT&keyword=$inpText",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 2,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer $tkn"
  ),
));

$response = curl_exec($curl);
curl_close($curl);
$result = json_decode($response, true);

//print_r ($result);
$count = $result['meta']['count'];
if($count == 0){
echo"<a class='list-group-item list-group-item-action border-1'><b>No record found</b></a>";
}else{
foreach($result['data'] as $element){
	$city     = $element['address']['cityName'];
	$country  = $element['address']['countryName'];
	$cityCode = $element['address']['cityCode'];
	$countryCode = $element['address']['countryCode'];
	$countCode = strtolower("$countryCode");
	$airportName = $element['name'];
	$iataCode = $element['iataCode'];
	
	
	
	echo"<a onclick=\"fromcity('$iataCode');\" class='list-group-item list-group-item-action border-1'>&nbsp;<img src='https://flagcdn.com/16x12/$countCode.png' width='20' height='15'><strong>($iataCode)$city,$country</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;$airportName</a>";
	 }}}





if (isset($_POST['querys'])) {
$inpTexts = $_POST['querys'];
$curl = curl_init();
$tkn = "$access_token";
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://test.api.amadeus.com/v1/reference-data/locations?subType=CITY,AIRPORT&keyword=$inpTexts",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 2,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer $tkn"
  ),
));

$response = curl_exec($curl);
curl_close($curl);
$results = json_decode($response, true);

//print_r ($result);
$counts = $results['meta']['count'];
if($counts == 0){
echo"<li class='list-group-item list-group-item-action border-1'><b>No record found</b></li>";
}else{
foreach($results['data'] as $elements){
	$citys     = $elements['address']['cityName'];
	$countrys  = $elements['address']['countryName'];
	$cityCodes = $elements['address']['cityCode'];
	$countryCodes = $elements['address']['countryCode'];
	$countCodes = strtolower("$countryCodes");
	$airportNames = $elements['name'];
	$iataCodes = $elements['iataCode'];
	
	
	
	echo"<li onclick=\"toCity('$iataCodes');\" class='list-group-item list-group-item-action border-1'>&nbsp;<img src='https://flagcdn.com/16x12/$countCodes.png' width='20' height='15'><strong>($iataCodes)$citys,$countrys</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;$airportNames</li>";
	 }}} ?>
	 
	
	 