Real-time Flight Status and Global Aviation Data with Flight Tracker API using PHP

The Aviation Data API allows the developer to get the real-time status of the flights. The flight tracker API is very useful to integrate real-time or historical flight information on the web application. You can easily track the flight status and access the airport timetable data using Flight Radar API. It returns real-time information about the flight status with the airport timetable. With this API you can get the current or historical data of the flights.

There are various Flight Tracker API available to fetch the flight status, aviationstack is one of the best free Flight Radar API among them. Aviationstack API provides an easy way to access global aviation data (flight status & airport timetable) in realtime. Aviationstack tracks each and every flight in the world at all time, store into the database and provide the real-time flight status through API. Aviationstack is easy-to-use REST API that returns the response in JSON format, it helps to use this API with any programming languages (PHP, Python, Ruby, Nodejs, jQuery, Go, etc.). In this tutorial, we will show you how to get the real-time flight status with aviationstack Flight Tracker API using PHP.

Follow the below simple steps to integrate Aviation Data API with aviationstack in PHP.

Get API Access Key

  • Before getting started, create an account on aviationstack.
  • In the dashboard, you will get the API key under the Your API Access Key.
    aviation-data-api-access-key-codexworld

API Configuration

The Access Key is required to authenticate and access the aviationstack API.

  • Build the query string using http_build_query() function to pass required params in the aviationstack API.
  • Specify the API Access Key in the access_key parameter.
$queryString http_build_query([ 
    
'access_key' => 'YOUR_ACCESS_KEY'
]);

Make HTTP GET Request

To fetch the aviation data, call aviationstack API via HTTP GET request using cURL in PHP.

// API URL with query string 
$apiURL sprintf('%s?%s''http://api.aviationstack.com/v1/flights'$queryString);

// Initialize cURL
$ch curl_init();

// Set URL and other appropriate options
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$api_response curl_exec($ch);

// Close cURL
curl_close($ch);

HTTPS Encryption:
To make secure API requests use HTTPS (SSL) encryption by calling API URL begins with an https.

https://api.aviationstack.com

Flight Status and Global Aviation Data

After a successful API request, the real-time flight status and aviation data will be returned as JSON format. Initially, the ipstack API returns the following geolocation data.

  • Flight Date (flight_date)
  • Flight Status (flight_status)
  • Departure and Arrival Info (departure/arrival)
    • airport
    • timezone
    • iata
    • icao
    • terminal
    • gate
    • delay
    • scheduled
    • estimated
    • actual
    • estimated_runway
    • actual_runway
  • Airline Info (airline)
    • name
    • iata
    • icao
  • Flight Info (flight)
    • number
    • iata
    • icao
    • codeshared
  • Aircraft Info (aircraft)
    • registration
    • iata
    • icao
    • icao24
  • Live Data (live)
    • updated
    • latitude
    • longitude
    • altitude
    • direction
    • speed_horizontal
    • speed_vertical
    • is_ground

Use json_decode() function to convert the JSON response to array in PHP.

// Convert API json response to array 
$api_result json_decode($api_responsetrue);

Example Code to Fetch Flight Status Data via aviationstack API

The complete code is given below to get global flight information with aviationstack API using PHP.

<?php 

// Set API access key 
$queryString http_build_query([
    
'access_key' => 'YOUR_ACCESS_KEY',
    
'limit' => 10  
]);

// API URL with query string
$apiURL sprintf('%s?%s''http://api.aviationstack.com/v1/flights'$queryString);

// Initialize cURL
$ch curl_init();

// Set URL and other appropriate options
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$api_response curl_exec($ch);

// Close cURL
curl_close($ch);

// Convert API json response to array
$api_result json_decode($api_responsetrue);

// Output of the Flights data
foreach ($api_result['data'] as $flight) {
    if (!
$flight['live']['is_ground']) {
        echo 
sprintf("%s flight %s from %s (%s) to %s (%s) is in the air.",
            
$flight['airline']['name'],
            
$flight['flight']['iata'],
            
$flight['departure']['airport'],
            
$flight['departure']['iata'],
            
$flight['arrival']['airport'],
            
$flight['arrival']['iata']
            ), 
PHP_EOL;
        echo 
'<br/>';
    }
}

?>

Conclusion

The aviationstack API is free to use, there also premium plans are available for advanced uses. In the example code, we have used some required parameters on API call. Various configuration options are available in aviationstack API, you can use these settings to customize the aviation data (flight status, airport timetable, etc.) as per your needs. For a complete reference, see the documentation of aviationstack API.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up