Skip to main content

Flight Data using Aviationstack API with PHP

Flights or Aviation data is a real-time flights data with information such as flight status, flight date, departure, arrival, routes, airline name and much more. The accurate, reliable and consistent aviation data is very useful and it is used by international organizations, aviation industry, tourism and other stakeholders.

So if you’re running a business or a developer and need accurate aviation data, then you’re here at the right place. In this tutorial, you will learn how to integrate Aviationstack API with PHP into your application to get the real-time aviation data.

The Aviationstack API provides simple way of accessing global aviation data for real-time and historical flights. It also allow customers to tap into an extensive data set of airline routes and other up-to-date aviation-related information. The API returns response data into JSON format and can be integrated using any programming language.

Also, read:

We will cover the tutorial step by step to integrate the API using PHP with example code. So let’s start coding.

Step1: Get API Access Key

First you need to create an account on Aviationstack. After creating an account, you will be able to retrieve your unique API access key using your account dashboard. The API Access Key can be used to connect to the API to get the data.

https://api.aviationstack.com/v1/flights?access_key = YOUR_ACCESS_KEY

Step2: Build Search Query

We will build the search query using API Access key to get the aviation data. We will pass the flight_date field with date to get the specific date data.

<?php

$date = '2019-12-11';

$query = http_build_query([
  'access_key' => 'YOUR_ACCESS_KEY',
  'flight_date' => $date
]);

?>

Step3: Make HTTP Request to API

To get the flight data, we need to make HTTP request to the API. So we will make HTTP request to the Aviationstack API using PHP Curl library by passing query. We will get the response data and store into $response variable. We will encode the response JSON data using json_decode() PHP function and store into $result variable.

<?php

$ch = curl_init(sprintf('%s?%s', 'https://api.aviationstack.com/v1/flights', $query));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);

?>

Step4: Complete Code

Here is the complete code to integrate the Aviationstack API with PHP to get the real-time aviation data.

<?php

$date = '2019-12-11';

$query = http_build_query([
  'access_key' => 'YOUR_ACCESS_KEY',
  'flight_date' => $date
]);

$ch = curl_init(sprintf('%s?%s', 'https://api.aviationstack.com/v1/flights', $query));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);

var_dump($result);

?>

Below is the response in JSON data format after running above example code.

{
    "pagination": {
        "limit": 100,
        "offset": 0,
        "count": 100,
        "total": 1669022
    },
    "data": [
        {
            "flight_date": "2019-12-11",
            "flight_status": "active",
            "departure": {
                "airport": "San Francisco International",
                "timezone": "America/Los_Angeles",
                "iata": "SFO",
                "icao": "KSFO",
                "terminal": "2",
                "gate": "D11",
                "delay": 13,
                "scheduled": "2019-12-11T04:20:00+00:00",
                "estimated": "2019-12-11T04:20:00+00:00",
                "actual": "2019-12-11T04:20:13+00:00",
                "estimated_runway": "2019-12-11T04:20:13+00:00",
                "actual_runway": "2019-12-11T04:20:13+00:00"
            },
            "arrival": {
                "airport": "Dallas/Fort Worth International",
                "timezone": "America/Chicago",
                "iata": "DFW",
                "icao": "KDFW",
                "terminal": "A",
                "gate": "A22",
                "baggage": "A17",
                "delay": 0,
                "scheduled": "2019-12-11T04:20:00+00:00",
                "estimated": "2019-12-11T04:20:00+00:00",
                "actual": null,
                "estimated_runway": null,
                "actual_runway": null
            },
            "airline": {
                "name": "American Airlines",
                "iata": "AA",
                "icao": "AAL"
            },
            "flight": {
                "number": "1004",
                "iata": "AA1004",
                "icao": "AAL1004",
                "codeshared": null
            },
            "aircraft": {
               "registration": "N160AN",
               "iata": "A321",
               "icao": "A321",
               "icao24": "A0F1BB"
            },
            "live": {
                "updated": "2019-12-11T10:00:00+00:00",
                "latitude": 36.28560000,
                "longitude": -106.80700000,
                "altitude": 8846.820,
                "direction": 114.340,
                "speed_horizontal": 894.348,
                "speed_vertical": 1.188,
                "is_ground": false
            }
        }, 
        [...]
    ]
}

Step5: Conclusion

In this tutorial we have explained how to integrate the Aviationstack API with PHP to get the real-time flight data. You can checkout the documentation for more advanced options and data.

You may also like: