How to Track Flight Status in real-time using the Flight Tracker API
Tags: php, api, json, development
Published: 2023-06-06 09:21:00

---
title: How to Track Flight Status in real-time using the Flight Tracker API
published: true
description: A Guide to utilizing the Flight Tracker API
date: 2023-06-06 09:21 UTC
tags: php, api, json, development
canonical_url: https://www.ma-no.org/en/programming/php/how-to-track-flight-status-in-real-time-using-the-flight-tracker-api
cover_image: https://www.ma-no.org/cache/galleries/contents-2297/960-400/flight-track-api-impjpg.webp
# Use a ratio of 100:42 for best results.
# published_at:  2023-06-06 09:21 +0000
---

<p>
</p>
<p>The Flight Tracker API provides developers with the ability to access real-time flight status, which is extremely useful for integrating historical tracking or live queries of air traffic into your website.
</p>
<p>With this API, you can easily track the status of a flight and access airport schedules.
</p>
<p>There are several Flight Tracker APIs available to retrieve flight status, and one of the best options is aviationstack. This API offers a simple way to access aviation data globally, including real-time flight status and airport schedules.
</p>
<p>Aviationstack tracks every flight worldwide at all times, storing the information in its database and providing real-time flight status through its API. It is a user-friendly REST API that returns responses in JSON format and is compatible with various programming languages such as PHP, Python, Ruby, Node.js, jQuery, Go, and more.
</p>
<p>In this tutorial, we will show you how to obtain real-time flight status using the aviationstack Flight Tracker API with PHP.
</p>
&nbsp;
<h3>Obtaining API Credentials
</h3>
&nbsp;
<p>To get started, you need to create an account on aviationstack. Once you are in your dashboard, you can copy the API access key from the &quot;<strong>Your API Access Key</strong>&quot; section.
</p>
&nbsp;
<h3>API Configuration
</h3>
&nbsp;
<p>We will need the access key to authenticate and access the <a href="aviationstack.com/product" rel="nofollow noreferrer noopener" target="_blank">aviationstack</a> API.
</p>
<p>Next, we will build the query using the 
  <code>http_build_query() 
  </code>&nbsp;function to pass the necessary parameters to the aviationstack API.
</p>
<p>Define the access key in the 
  <code>access_key 
  </code>&nbsp;parameter.
</p>
&nbsp;
<pre>
$queryString = http_build_query([
&#39;access_key&#39; =&gt; &#39;YOUR_ACCESS_KEY&#39;
]);
</pre>
&nbsp;
<h3>HTTP GET Request
</h3>
&nbsp;
<p>To retrieve flight data, we will make an 
  <code>HTTP GET 
  </code> request to the aviationstack API using cURL.
</p>
&nbsp;
<pre>
// API URL with the query string
$apiURL = sprintf(&#39;%s?%s&#39;, &#39;https://api.aviationstack.com/v1/flights&#39;, $queryString);
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the API request
$api_response = curl_exec($ch);
// Close cURL
curl_close($ch);
</pre>
&nbsp;
<p>It is important to ensure that the API call is secure, so we should use the URL with 
  <code>https 
  </code>:
</p>
<p><a href="https://api.aviationstack.com" rel="nofollow noreferrer noopener" target="_blank">https://api.aviationstack.com</a>
</p>
&nbsp;
<h3>Flight Status and General Information
</h3>
&nbsp;
<p>After making the API call, we will receive the current flight status and related information in JSON format. Initially, the aviationstack API provides the following geolocation data:
</p>
<p>- Flight date (flight_date)
  <br />
  - Flight status (flight_status)
  <br />
  - Departure and arrival information (departure/arrival)
  <br />
  - airport
  <br />
  - timezone
  <br />
  - iata
  <br />
  - icao
  <br />
  - terminal
  <br />
  - gate
  <br />
  - delay
  <br />
  - scheduled
  <br />
  - estimated
  <br />
  - actual
  <br />
  - estimated_runway
  <br />
  - actual_runway
</p>
<p>- Airline information (airline)
  <br />
  - name
  <br />
  - iata
  <br />
  - icao
</p>
<p>- Flight information (flight)
  <br />
  - number
  <br />
  - iata
  <br />
  - icao
  <br />
  - codeshared
</p>
<p>- Aircraft information (aircraft)
  <br />
  - registration
  <br />
  - iata
  <br />
  - icao
  <br />
  - icao24
</p>
<p>- Live data (live)
  <br />
  - updated
  <br />
  - latitude
  <br />
  - longitude
  <br />
  - altitude
  <br />
  - direction
  <br />
  - speed_horizontal
  <br />
  - speed_vertical
  <br />
  - is_ground
</p>
<p>By using the 
  <code>json_decode()&nbsp; 
  </code>function, we can convert the obtained JSON data into a PHP array.
</p>
<p>Here&#39;s an example of how to extract flight information using the aviationstack API:
</p>
&nbsp;
<pre>
$api_result = json_decode($api_response, true);
</pre>
&nbsp;
<p>Below is the complete code to retrieve global flight information using the aviationstack API with PHP:
</p>
&nbsp;
<pre><br>// Define the API access key<br>$queryString = http_build_query([<br>    'access_key' =&gt; 'YOUR_ACCESS_KEY',<br>    'limit' =&gt; 10<br>        ]);<br>// API URL with the query string<br>$apiURL = sprintf('%s?%s', 'https://api.aviationstack.com/v1/flights', $queryString);<br>// Initialize cURL<br>$ch = curl_init();<br>curl_setopt($ch, CURLOPT_URL, $apiURL);<br>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>// Execute the API request<br>$api_response = curl_exec($ch);<br>// Close cURL<br>curl_close($ch);<br>// Convert the JSON into an array<br>$api_result = json_decode($api_response, true);<br>// Display flight data<br>foreach ($api_result['data'] as $flight) {<br>  if (!$flight['live']['is_ground']) {<br>    echo sprintf(&quot;%s flight %s from %s (%s) to %s (%s) is in the air.&quot;,<br>            $flight['airline']['name'],<br>            $flight['flight']['iata'],<br>            $flight['departure']['airport'],<br>            $flight['departure']['iata'],<br>            $flight['arrival']['airport'],<br>            $flight['arrival']['iata']<br>    ), PHP_EOL;<br>    echo '&lt;br/&gt;';<br>  }<br>}<br>?&gt;<br>
</pre>
&nbsp;
<p>This code allows you to retrieve updated flight information using the aviationstack API. Make sure to replace &#39; 
  <code>YOUR_ACCESS_KEY 
  </code>&#39; with your provided personal access key from aviationstack.
</p>
<p>You can integrate this code into your PHP application to display real-time flight status and related details on your website.
</p>
<p>Note: It&#39;s important to handle error cases, such as when the API response is not successful or when there are no flight data available. You can add error handling and additional logic as per your application requirements.
</p>