This request is useful for retrieving all of the data about a single airport. The only parameter necessary is the iata code of the airport. No jQuery or other frameworks are required to use our Air-port-codes library.
The basics
Here are the necessary parts to getting things working. First load our apc library that you downloaded earlier. Then initialize the api library, with the first parater being 'countries', signifying the api you want to access. The second parameter being your api key and any other necessary parameters. Setup your callbacks (onSuccess, onError). After that, you are ready to make your request.
- <script src="air-port-codes-api-min.js"></script>
- <script>
- var apiKey = 'xxxxxxxxxx';
-
- var params = {
- key: apiKey, // this is required and can be found in your Air-port-codes account.
- secret: 'xxxxxxxxxxxxxxx', // the secret key, necessary for requests that don't provide a referrer
- is_select_options: true, // will return the countries object as a string of options for a select tag
- autoselect_visitor_location: true, // this will allow you to have the persons country auto selected in a select field
- field_name: 'states' // Use this if you want the response to be returned with the select html tag wrapping it with the name attribute being set to this value. This is useful, especially when the country doesn't have any states associated with it, an input field will be returned instead of a select field.
- // selected: 'AU', // this will select the specified country in the select field
- // ip_address: '72.229.28.185' // this will select the specified country from the ip address
- }
- // initialize the apc library
- var apcc = new apc('countries', params);
-
- // handle successful response
- apcc.onSuccess = function (data) {
- console.log(data.airports);
- };
-
- // handle response error
- apcc.onError = function (data) {
- console.log(data.message);
- };
-
- // makes the request to get the airport data
- apcc.request();
- </script>
Depending on your 'is_select_options' parameter, your response in your onSuccess function will either contain an object of countries or an option list for a select field.
If you use our PHP library, included in the download above, then it will be very easy to get a response back from the Air-port-codes API. All of the curl functionality will be handled for you.
You start by including the apc class. Open up the class and set the API Key. You can set your API Secret Key there as well, if you need to.
- require_once('air-port-codes-api.php');
Next you create an array of parameters that will be used in your request to the API. Then pass in the parameters when you instantiate the apc class.
- // Setup request parameters to override the configparams set in apc class
- $params = [
- 'key' => 'xxxxxxxxxx', // this is required and can be found in your Air-port-codes account.
- // 'secret' => 'xxxxxxxxxxxxxxx', // the secret key, necessary for requests that don't provide a referrer
- is_select_options: true, // will return the countries object as a string of options for a select tag
- field_name: 'states' // Use this if you want the response to be returned with the select html tag wrapping it with the name attribute being set to this value. This is useful, especially when the country doesn't have any states associated with it, an input field will be returned instead of a select field.
- // selected: 'AU', // this will select the specified country in the select field
- // ip_address: '72.229.28.185' // this will select the specified country from the ip address
- ];
-
- $apc = new apc('countries', $params);
The final step is simply making the request. It will manage the curl functionality for you.
- $apcResponseObj = $apc->request();
You can now display the results however you want. The returned object will be a json decoded object or an html list of options for a select field, depending on your 'is_select_options' parameter.
- <pre>
- <?= print_r($apcResponseObj->countries); ?>
- </pre>