Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow

StackOverflow Logo StackOverflow Logo

StackOverflow Navigation

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Add group
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 680
Next

StackOverflow Latest Questions

Saralyn
  • 0
  • 0
SaralynBegginer
Asked: April 10, 20252025-04-10T20:11:34+00:00 2025-04-10T20:11:34+00:00In: PHP

php – Amadeus Airport & City Autocomplete Form On WordPress

  • 0
  • 0
php – Amadeus Airport & City Autocomplete Form On WordPress

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=true

Any 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 Number

Next step:
Contact Details

And 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.

0
  • 0 0 Answers
  • 45 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • W3spoint99

    What is Physics? Definition, History, Importance, Scope (Class 11)

    • 1 Answer
  • W3spoint99

    The Living World – Introduction, Classification, Characteristics, FAQs (Class 11 ...

    • 1 Answer
  • W3spoint99

    Explain - Biological Classification (Class 11 - Biology)

    • 1 Answer
  • Saralyn
    Saralyn added an answer When Humans look at their childhood pictures, the first thing… January 17, 2025 at 3:25 pm
  • Saralyn
    Saralyn added an answer Previously, length was measured using units such as the length… January 17, 2025 at 3:25 pm
  • Saralyn
    Saralyn added an answer Measurement forms the fundamental principle to various other branches of… January 17, 2025 at 3:25 pm

Related Questions

  • Reading fancy apostrophe PHP [duplicate]

    • 0 Answers
  • Unable to send mail via PHPMailer [SMTP->Error: Password not accepted ...

    • 0 Answers
  • Concerns when migrating from PHP 5.6 to 8.4 [closed]

    • 0 Answers
  • Laravel Auth::attempt() error: "Unknown column 'password'" when using a custom ...

    • 0 Answers
  • Core PHP cURL - header origin pass null value

    • 0 Answers

Trending Tags

biology class 11 forces how physics relates to other sciences interdisciplinary science learn mathematics math sets tutorial null sets physics physics and astronomy physics and biology physics and chemistry physics applications science science connections science education sets in mathematics set theory basics types of sets types of sets explained

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

  • About US
  • Privacy Policy
  • Questions
  • Recent Questions
  • Web Stories

© 2025 WikiQuora.Com. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.