Skip to main content

Get Geocoding using Positionstack API with PHP

Geocoding is a way to display address, name of place, latitude/langitude, IP address into the location on the earth surface for that place. The Geocoding handled as forward geocoding and reverse geocoding. In farword geocoding, the location address text pass as parameter to get the geocoding while in reverse geocoding, the latitude/langutude or ip address passed to get the geocoding.

Geocoding data is very useful as it help to know the details with map of particular address or IP address. Due to importance, many businesses always looking for the accurate geocoding data. But it’s not very easy to get the accurate and reliable Geocoding data.

So if you’re running a business or a developer and looking for the geocoding data, then you’re here at the right place. In this tutorial, you will learn how to integrate the Positionstack with PHP to get the Geocoding.

Positionstack API provides accurate and reliable geocoding data with maps URL to use into your application. The API return response data into JSON, XML, GeoJSON format and can be integrated using any programming language.

Also, read:

We will cover this tutorial step by step to integrate the API with example code. So let’s start coding.

Step1: Get API Access Key

For accessing the Positionstack API, we need to get API Access Key to access. So first we will createan account on Positionstack API to get the API Access Key.

We will use the API Access Key with API request URL. In below code, we are making request for the forward geocoding.

https://api.positionstack.com/v1/forward ? access_key = YOUR_ACCESS_KEY

We need to pass the reverse in place of forward when we will make reverse Geocoding request like below.

https://api.positionstack.com/v1/reverse ? access_key = YOUR_ACCESS_KEY

Step2: Build Search Query with Options

We need to create search query with options to make the HTTP API request. We will pass the access_key field which is mandatory. We will also pass the query field with location address for forward geocoding request.

<?php

$searchTerm = '1600 Pennsylvania Ave NW, Washington DC';

$buildQuery = http_build_query([
  'access_key' => 'YOUR_ACCESS_KEY',
  'query' => $searchTerm
]);

?>

We need to pass the latitude/longitude or IP address to location data when we will make request for reverse geocoding like below.

<?php

$searchTerm = '40.7638435,-73.9729691';

$buildQuery = http_build_query([
  'access_key' => 'YOUR_ACCESS_KEY',
  'query' => $searchTerm
]);

?>

Step3: Make HTTP Request to API

We will make HTTP request to the Positionstack API using PHP Curl library to get Geocoding data. Here we will make forward Geocoding request with address string.

<?php

$ch = curl_init(sprintf('%s?%s', 'https://api.positionstack.com/v1/forward', $buildQuery));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$responseData = curl_exec($ch);
curl_close($ch);

$resultData = json_decode($responseData, true);

?>

We will pass the reverse with request URL when we will make reverse Geocoding request like below.

<?php

$ch = curl_init(sprintf('%s?%s', 'https://api.positionstack.com/v1/reverse', $buildQuery));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$responseData = curl_exec($ch);
curl_close($ch);

$resultData = json_decode($responseData, true);

?>

Step4: Complete Code for Forward Geocoding Request

Here is the complete code for the forward Geocoding API request to get response data.

<?php

$searchTerm = '1600 Pennsylvania Ave NW, Washington DC';

$buildQuery = http_build_query([
  'access_key' => 'YOUR_ACCESS_KEY',
  'query' => $searchTerm
]);

$ch = curl_init(sprintf('%s?%s', 'https://api.positionstack.com/v1/forward', $buildQuery));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$responseData = curl_exec($ch);
curl_close($ch);

$resultData = json_decode($responseData, true);

var_dump($resultData);

?>

Below is the response in JSON data format after running above example code.

{
   "data": {
      "results": [
         {
            "latitude": 38.897675,
            "longitude": -77.036547,
            "label": "1600 Pennsylvania Avenue NW, Washington, DC, USA",
            "name": "1600 Pennsylvania Avenue NW",
            "type": "address",
            "number": "1600",
            "street": "Pennsylvania Avenue NW",
            "postal_code": "20500",
            "confidence": 1,
            "region": "District of Columbia",
            "region_code": "DC",
            "administrative_area": null,
            "neighbourhood": "White House Grounds",
            "country": "United States",
            "country_code": "US",
            "map_url": "http://map.positionstack.com/38.897675,-77.036547"
         }
      ]
   }
}

Step5: Complete Code for Reverse Geocoding Request

Here is the complete code for the reverse Geocoding to get Geocoding response data.

<?php

$serachTerm = '40.7638435,-73.9729691';

$buildQuery = http_build_query([
  'access_key' => 'YOUR_ACCESS_KEY',
  'query' => $serachTerm
]);

$ch = curl_init(sprintf('%s?%s', 'https://api.positionstack.com/v1/reverse', $buildQuery));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$responseData = curl_exec($ch);
curl_close($ch);

$resultData = json_decode($responseData, true);

var_dump($resultData);

?>

Below is the response in JSON data format after running above example code.

{
   "data": {
      "results": [
         {
            "latitude": 40.763841,
            "longitude": -73.972972,
            "label": "Apple Store, Manhattan, New York, NY, USA",   
            "name": "Apple Store",
            "type": "venue",
            "distance": 0,
            "number": "767",
            "street": "5th Avenue",
            "postal_code": "10153",
            "confidence": 1,
            "region": "New York",
            "region_code": "NY",
            "administrative_area": null,
            "neighbourhood": "Midtown East",
            "country": "United States",
            "country_code": "US",
            "map_url": "http://map.positionstack.com/40.763841,-73.972972",
         }
      ]
   }
}

Step6: Conclusion

In this tutorial we have explained how to integrate Positionstack API to get forward and reverse Geocoding data using PHP. You can also checkout the documentation for more advanced API options and data.

You may also like: