States of every country request - installation

This request can be used to simply retrieve all of the state/province information from a given country. You can use it to return an array of values or you can even retrieve them as options for a form select field (demo) with the current users state/province preselected.

This request can be used on its own, or even in conjunction with our countries list as you can see from our demo.

Download our example jQuery code, it comes with JavaScript and PHP libraries that make connecting to our API a breeze. Your API key can be found in your air-port-codes.com account and is necessary to be granted access privileges to any of our API's.

Use our interactive REST tool (Swagger) to help you test the api and build your queries the right way. You'll need to login before you can use it with your API key.

Quick start

First login to your air-port-codes.com account and update your account settings by adding any domains that might need to access the API. Also make a note of your API Key as you will need it for all requests to the API.

Javascript

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
    country: 'US' // the 2-letter iso of the requested country
    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.
    autoselect_visitor_location: true, // this will allow you to have the persons country auto selected in a select field
    // selected: 'NY', // 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('states', 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.

PHP

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 = [
    country: 'US' // the 2-letter iso of the requested country
    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('states', $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->states); ?>
</pre>

Need assistance?

If you need assistance getting our feed to work or simply have a suggestion, feel free to fill out our contact form.