Aviation data is important as it’s used to know the real-time flight status, airline routes and other flight related information. There are many companies that’s always looking for the aviation data to know the historical flights data, airport timetable, airlines routes etc. But it’s not easy to get the real-time aviation data. It’s always a challenge to get the accurate aviation data.
So if you’re running a running a business or a programmer and looking for the real-time aviation data to use in your application, then you’re here at the right place. In this tutorial you will learn how to get the real-time aviation data using Aviationstack API. The Aviationstack API is free to use and can easily integrated with any major programming language. The API returns response data into light-weight JSON data format.
Here in this tutorial, we will integrate the Aviationstack API with PHP to get the real-time flight data. We will cover the tutorial step by step to get the API key and integrate the API to get the real-time data. So let’s proceed to integrate the API.
Step1: Get Aviationstack API Key
First we need to register to the Aviationstack API to get the API access key. We will use the API Access Key with Aviationstack API request URL to get the flights data.
https://api.aviationstack.com/v1/flights ? access_key = YOUR_ACCESS_KEY
Step2: Create Flight Data Search Query
We will create data search query using API Access key to get the aviation data. The API Access key is the mandatory field. There are also optional fields such as limit, offset, flight_date etc. to pass according to requirement to get the flight data. You can check the documentation for the more options.
Here we will pass the flight date to get the data for the specific date.
<?php $flightDate = '2019-12-12'; $searchQuery = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'flight_date' => $flightDate ]); ?>
Step3: Make HTTP Request to Aviationstack API
Now we will make HTTP request to the Aviationstack API using PHP Curl library by passing query string and get the response data. We will store the response data into $responseData variable.
<?php $ch = curl_init(sprintf('%s?%s', 'https://api.aviationstack.com/v1/flights', $searchQuery)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $responseData = curl_exec($ch); curl_close($ch); ?>
Step4: Display Real-time Aviation Data
We will decode the response data using PHP json_decode() function and store into $resultData variable. We will loop through the response result data and display the flights details.
<?php $resultData = json_decode($responseData, true); foreach ($resultData['results'] 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; } } ?>
Step5: Complete Code
Here is the complete code to integrate the Aviationstack API with PHP to get the real-time aviation data.
<?php $flightDate = '2019-12-12'; $searchQuery = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'flight_date' => $flightDate ]); $ch = curl_init(sprintf('%s?%s', 'https://api.aviationstack.com/v1/flights', $searchQuery)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $responseData = curl_exec($ch); curl_close($ch); $resultData = json_decode($responseData, true); var_dump($resultData); ?>
The response JSON data would be following after running above code.
{ "pagination": { "limit": 100, "offset": 0, "count": 100, "total": 1669022 }, "data": [ { "flight_date": "2019-12-12", "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-12T04:20:00+00:00", "estimated": "2019-12-12T04:20:00+00:00", "actual": "2019-12-12T04:20:13+00:00", "estimated_runway": "2019-12-12T04:20:13+00:00", "actual_runway": "2019-12-12T04: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-12T04:20:00+00:00", "estimated": "2019-12-12T04: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-12T10: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 } }, [...] ] }
Step6: Conclusion
In this tutorial you have learned how to integrate the Aviationstack API to get the real-time flights data. For more advanced options, you can checkout the documentation.
You may also like:
- Get Geolocation Data with IPWHOIS.IO API
- IP Geolocation API Integration with PHP
- Geocoding using Positionstack API with PHP
- User Agent Lookup using Userstack API with PHP
- Reverse IP Lookup using ipapi with PHP
- Weather Data with Weatherstack API using PHP
- Website Visitors IP Lookup with IPStack API using PHP
- Scraping Review Data using ReviewAPI with PHP
- Scrape SERP Data with SerpStack API using PHP
- Scraping Review Data using ReviewAPI with PHP
- Search Result Scraping with Zenserp API using PHP
- Web Scraping with Zenscrape API using PHP
- Web Scraping with Real-Time ScrapeStack REST API using PHP