Forward and Reverse Batch Geocoding API with PHP

Geocoding is the process that allows converting an address into geographic coordinates. There are two types of geocoding, Forward and Reverse. Forward geocoding is the process of finding associated geographic coordinates (latitude and longitude) from a given address. Reverse geocoding is the process of finding an address from a given coordinate (latitude and longitude). The Geocoding API provides an easy way to convert geographic coordinates into the human-readable addresses and retrieve coordinates from the address.

There are various Geocoding API available for Forward and Reverse geocoding, positionstack API is one of the best free Geocoding API among them. Positionstack API provides an easy-to-use solution forward and reverse geocoding. Positionstack API offers geocoding REST API that enables you to do the following:

  • Forward Geocoding
  • Reverse Geocoding
  • Batch Requests
  • Embeddable Maps
  • Multiple Languages

The positionstack Geocoding API can be used with any programming languages (PHP, Python, Ruby, jQuery, Nodejs, Go, etc.). In this tutorial, we will show you how to get coordinates data from address and address from geographic coordinates with positionstack geocoding API using PHP.

Follow the below simple steps to integrate positionstack API in PHP.

Get API Access Key

  • Before getting started, create an account on positionstack.
  • In the dashboard, you will get the API key under the Your API Access Key.
    forward-reverse-geocoding-api-access-key-codexworld

API Configuration

The Access Key is required to authenticate and access the positionstack API.

  • Build the query string using http_build_query() function to pass required params in the positionstack API.
  • Specify the API Access Key in the access_key parameter.
  • Specify the coordinates/address in the query parameter.

Forward Geocoding:

$queryString http_build_query([ 
  
'access_key' => 'YOUR_ACCESS_KEY'
  
'query' => '1600 Pennsylvania Ave NW',
  
'region' => 'Washington',
  
'output' => 'json',
  
'limit' => 1,
]);

Reverse Geocoding:

$queryString http_build_query([ 
  
'access_key' => 'YOUR_ACCESS_KEY'
  
'query' => '48.2084,16.3731',
  
'language' => 'ES',
]);

Forward Geocoding with PHP

To get the coordinates data from address, call positionstack API via HTTP GET request using cURL in PHP.

// API URL with query string  
$apiURL sprintf('%s?%s''http://api.positionstack.com/v1/forward'$queryString); 
 
// Create a new cURL resource 
$ch curl_init(); 
 
// Set URL and other appropriate options 
curl_setopt($chCURLOPT_URL$apiURL); 
curl_setopt($chCURLOPT_RETURNTRANSFERtrue); 
 
// Execute and get response from API 
$api_response curl_exec($ch); 
 
// Close cURL resource 
curl_close($ch);

HTTPS Encryption:
To make secure API requests use HTTPS (SSL) encryption by calling API URL begins with an https.

https://api.positionstack.com/v1/forward

Reverse Geocoding with PHP

To get the address from coordinates, call positionstack API via HTTP GET request using cURL in PHP.

// API URL with query string  
$apiURL sprintf('%s?%s''http://api.positionstack.com/v1/reverse'$queryString); 
 
// Create a new cURL resource 
$ch curl_init(); 
 
// Set URL and other appropriate options 
curl_setopt($chCURLOPT_URL$apiURL); 
curl_setopt($chCURLOPT_RETURNTRANSFERtrue); 
 
// Execute and get response from API 
$api_response curl_exec($ch); 
 
// Close cURL resource 
curl_close($ch);

HTTPS Encryption:
To make secure API requests use HTTPS (SSL) encryption by calling API URL begins with an https.

https://api.positionstack.com/v1/reverse

Geographic Coordinates Data

After a successful API request, the coordinates/address information will be returned as JSON format. Use json_decode() function to convert the JSON response to array in PHP.

// Convert API json response to array  
$apiResult json_decode($api_responsetrue);

Conclusion

The positionstack API is free to use, there also premium plans are available for advanced uses. In the example code, we have used some required parameters on API call. Various configuration options are available in positionstack API, you can use these to customize the search result. For a complete reference, see the documentation of positionstack API.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up