Im trying to build a form which on submission will send an email to the site admin, and nothing else is required. So im using Amadeus’s API to get the Airport Cities filled in dynamically into their relevant fields, so that users only get suggestions from those cities.
Now For a start i starting to connect the API with my WordPress and try to see what data im getting in order to manage it furthur.
This is my code so far:
// Add the airport data request route to handle the Amadeus API call if (isset($_GET['airport_data']) && $_GET['airport_data'] == 'true') { add_action('template_redirect', 'generate_airport_data'); } function generate_airport_data() { // Enable error reporting ini_set('display_errors', 1); error_reporting(E_ALL); // Get the Amadeus API token (ensure you've authenticated with the API beforehand) $access_token = get_amadeus_api_token(); // You need to get your access token here if (!$access_token) { die('No API token available.'); } // Prepare the endpoint to fetch airports and cities $endpoint="https://test.api.amadeus.com/v1/reference-data/locations"; // $endpoint="https://test.api.amadeus.com/v1/reference-data/locations?subType=AIRPORT&keyword="; $params = array( 'subType' => 'AIRPORT', // Change this to 'CITY' for city search 'keyword' => 'A', // 'keyword' => '', // Get the search term from the URL ); // Build the query string $params_str = http_build_query($params); // Make the API request using WP's HTTP API $response = wp_remote_get($endpoint . '?' . $params_str, array( 'headers' => array( 'Authorization' => 'Bearer ' . $access_token, ), )); // Check for errors in the API request if (is_wp_error($response)) { die('API request failed: ' . $response->get_error_message()); } // Decode the response body $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); // Debugging: log full response echo 'check1
'; print_r($body); // Display the raw response for debugging echo '
'; // If the token is expired, force refresh and retry if (isset($data['errors'][0]['code']) && $data['errors'][0]['code'] == 38192) { delete_transient('amadeus_access_token'); // Clear expired token $access_token = get_amadeus_api_token(true); // Force token refresh die('Access token refreshed. Please try again.'); } // Return the API data if (isset($data['data'])) { echo json_encode($data['data']); // Return the city or airport data } else { echo json_encode([]); // Return an empty array if no data found } // End the script so it doesn't load the WordPress theme exit; } // Function to get the Amadeus API access token function get_amadeus_api_token($force_refresh = false) { // Retrieve token from cache if not forcing refresh if (!$force_refresh) { $access_token = get_transient('amadeus_access_token'); if ($access_token) { return $access_token; } } // Request a new access token $url="https://test.api.amadeus.com/v1/security/oauth2/token"; $auth_data = array( 'client_id' => '*******', 'client_secret' => '*******', 'grant_type' => 'client_credentials' ); $headers = array('Content-Type' => 'application/vnd.amadeus+json'); $response = wp_remote_post($url, array( 'body' => $auth_data, 'headers' => $headers, )); // Check if the request was successful if (is_wp_error($response)) { die('Token request failed: ' . $response->get_error_message()); } $response_body = json_decode(wp_remote_retrieve_body($response)); echo 'check2' . json_encode($response_body, JSON_PRETTY_PRINT) . '';
if (isset($response_body->error)) {
die('Error fetching token: ' . $response_body->error_description);
}// Store the new token in a transient (caches it for 1 hour)
$access_token = $response_body->access_token;
set_transient('amadeus_access_token', $access_token, 3600);return $access_token;
}But this is the result im getting:
check2 { "type": "amadeusOAuth2Token", "username": "[email protected]", "application_name": "Premier Travel Tours App", "client_id": "*********", "token_type": "Bearer", "access_token": "*********", "expires_in": 1799, "state": "approved", "scope": "" } check1 {"errors":[{"status":503,"code":19,"title":"No available service for processing the request.","source":{"pointer":"uri"}}]} []
You can see for yourselves here:
https://www.premiertravelandtourism.com/?airport_data=trueAny help will be appriciated, thanks in advance.
Im just trying to build a form that sends email with these details:
Origin City
Destination City
Departure Month/Year
Arrival Month/Year (If 2 way)
Passenger’s NumberNext step:
Contact DetailsAnd the on submit it will send me the email.
But i need to get the cities dynamically from a free API, so i chose Amadeus. And now im face 503 error.