|

|  How to Access Flight Information with Amadeus API in PHP

How to Access Flight Information with Amadeus API in PHP

October 31, 2024

Discover how to access flight information using the Amadeus API in PHP with our step-by-step guide and enhance your travel applications effortlessly.

How to Access Flight Information with Amadeus API in PHP

 

Install Required Libraries

 

  • Ensure that you have PHP installed and configured properly. You will need the `curl` extension enabled for API requests.
  •  

  • Use Composer to install the Amadeus PHP SDK, which simplifies the process of interacting with the Amadeus API:
composer require amadeus4dev/amadeus-php

 

Initialize the Amadeus Client

 

  • Import the Amadeus client into your PHP script and initialize it using your API credentials. Make sure to securely store these credentials to prevent unauthorized access.
require 'vendor/autoload.php';

use Amadeus\Amadeus;

$amadeus = Amadeus::builder("YOUR_API_KEY", "YOUR_API_SECRET")->build();

 

Access Flight Offers

 

  • You can access flight offers by sending requests with search criteria such as origin, destination, departure date, and return date. The server will respond with a list of available flight options.
try {
    $response = $amadeus->shopping->flightOffersSearch->get([
        'originLocationCode' => 'LAX',
        'destinationLocationCode' => 'JFK',
        'departureDate' => '2024-03-01',
        'returnDate' => '2024-03-10',
        'adults' => 1
    ]);

    print_r($response);
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

 

Handle API Responses

 

  • Process the API responses to extract useful information. You might need to parse JSON data and handle potential errors gracefully.
foreach ($response->getData() as $flight) {
    echo "Flight ID: " . $flight['id'] . "<br>";
    echo "Price: " . $flight['price']['total'] . " " . $flight['price']['currency'] . "<br>";
    echo "Departure: " . $flight['itineraries'][0]['segments'][0]['departure']['iataCode'] . " - ";
    echo "Arrival: " . $flight['itineraries'][0]['segments'][0]['arrival']['iataCode'] . "<br><br>";
}

 

Fetch Flight Status

 

  • To access real-time flight status, use the flight status API with a specified flight number and date.
try {
    $flightStatus = $amadeus->schedule->flights->get([
        'carrierCode' => 'AA',
        'flightNumber' => '100',
        'scheduledDepartureDate' => '2023-12-01'
    ]);

    print_r($flightStatus);
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

 

Implement Best Practices

 

  • Use caching mechanisms to store frequent API responses and reduce the number of requests to the API, minimizing latency and API costs.
  •  

  • Implement error handling to manage unexpected responses or network issues. Consider retry mechanisms for transient errors.
  •  

  • Limit the data requested by making efficient use of the API parameters and filtering options to ensure only necessary information is retrieved.