Geocoding is the process of transforming location such as location address, coordinates or IP address to a location on the earth surface. The Geocoding data covers everything related to that locations such as latitude, longitude, map URL, region, country, country code and much more.
Geocoding data plays an important role to know about the particular location and IP address. Due to it’s importance, most of the companies always looking for the accurate Geocoding data to use with their applications.
So if you’re running a business or developers and looking for accurate Geocoding data to implement in your application, then you’re here at the right place. In this tutorial, you will learn how integrate Geocoding Positionstack API with PHP to get the accurate Geocoding.
Positionstack API is a feature rich Geocoding API that provides reliable solution for forward and reverse Geocoding. The major features includes batch geocoding, multi-language support, embeddable map URLs, and more. The API response data can be get into JSON, XML or geocode-specific GeoJSON data.
We will cover this tutorial in easy steps to integrate the API with PHP. So let’s proceed with it.
Step1: Get Positionstack API Access Key
We need to get API Access Key to access the Positionstack API. So first we will create an account on Positionstack API to get the API Access Key.
https://api.positionstack.com/v1/forward ? access_key = YOUR_ACCESS_KEY
In above code, we are making request for the forward Geocoding. We will 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 Query with Options
We will build the query with options to pass with HTTP request to get the Geocoding data. We will pass the access_key which is mandatory field. We will also pass the query field with address string as we will request the forward Geocoding .
<?php $searchQuery = '1600 Pennsylvania Ave NW, Washington DC'; $buildQuery = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'query' => $searchQuery ]); ?>
We will pass the latitude & longitude or an IP address to location data when we will request for reverse Geocoding like below.
<?php $searchQuery = '40.7638435,-73.9729691'; $buildQuery = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'query' => $searchQuery ]); ?>
Step3: Make HTTP Request to Positionstack API
We will make HTTP request to the Positionstack API using PHP Curl library to get Geocoding data.
<?php $ch = curl_init(sprintf('%s?%s', 'https://api.positionstack.com/v1/forward', $buildQuery)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $result = json_decode($response, true); ?>
In above code, we are making forward Geocoding request. We need to pass 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); $response = curl_exec($ch); curl_close($ch); $result = json_decode($response, true); ?>
Step4: Complete Code for Forward Geocoding
Here is the complete code for the forward Geocoding to get response data.
<?php $searchQuery = '1600 Pennsylvania Ave NW, Washington DC'; $buildQuery = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'query' => $searchQuery ]); $ch = curl_init(sprintf('%s?%s', 'https://api.positionstack.com/v1/forward', $buildQuery)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $result = json_decode($response, true); var_dump($result); ?>
Below is the response in JSON data format from 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
Here is the complete code for the reverse Geocoding to get response data.
<?php $searchQuery = '40.7638435,-73.9729691'; $buildQuery = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'query' => $searchQuery ]); $ch = curl_init(sprintf('%s?%s', 'https://api.positionstack.com/v1/reverse', $buildQuery)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $result = json_decode($response, true); var_dump($result); ?>
Below is the response in JSON data format from 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 you have learned how to integrate Positionstack API to get forward and reverse Geocoding data. You can checkout the documentation for more advanced options and data.
You may also like:
- Get Geolocation Data with IPWHOIS.IO API
- IP Geolocation API Integration with PHP
- Get Real-time Flight Data using Aviationstack API with PHP
- User Agent Lookup using Userstack API with PHP
- Reverse IP Lookup using ipapi with PHP
- Weather Data with Weatherstack API using PHP
- Website Visitors IP Lookup with IPStack API using PHP
- Scraping Review Data using ReviewAPI with PHP
- Scrape SERP Data with SerpStack API using PHP
- Scraping Review Data using ReviewAPI with PHP
- Search Result Scraping with Zenserp API using PHP
- Web Scraping with Zenscrape API using PHP
- Web Scraping with Real-Time ScrapeStack REST API using PHP