Skip to main content

Get Address Information using Geocode API with PHP

Geocoding is refers to the conversion of addresses into geographic coordinates to get the address information. Getting the address information is important for any business. The accurate user information can help the websites to localize language on a website, display contents and target the users with related information.

So if you’re running a website or developing a website and want to implement the functionality to get the address information. Then you’re here at the right place. In this tutorial, you will learn how to integrate the Geocodeapi with PHP to get the address information.

The Geocodeapi is an easy to use, fast, scalable and reliable Geocoding and Geoparsing API, developed by SaaS Industries. You can easily integrate the API in your application and get the address information into your desired format such as JSON, raw HTML etc.

Also, read:

So let’s proceed with the integration of Geocode API with PHP to get the address information.

Step1: Get Geocode API

To access the Geocode API, we will need to sign up on Geocode API to create an account to get the API Key.

We will use the API Key to make the HTTP request to the API.

https://app.geocodeapi.io/api/v1/search?apikey=APIKEY

Step2: Create Search Query

We will create search query to get the Geocode data for a address. We will pass apikey parameter to pass the API key. We will also pass text parameter with the address text to search the data.

<?php 

$address = 'Madison Capital';
$apiKey = "YOUR_API_KEY";

$searchQuery = [
	'text' => $address,
	'apikey' => $apiKey,
];

?>

Step3: Make Request to Geocode API

We will make HTTP request to the Geocode API to get the Geocode data. So we will make HTTP request with PHP Curl function and pass the created search data query to search the Geocode data. We will get the response and stored into $response variable.

<?php 

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_URL, 'https://app.geocodeapi.io/api/v1/search?' . http_build_query($searchQuery));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"Content-Type: application/json",
	"apikey: $apiKey",
));

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

?>

Step4: Decode Response Data

Now we will decode the response data to JSON using PHP json_decode() function and store the final JSON result data into $result variable.

<?php

$result = json_decode($response);

var_dump($result);

?>

Step5: Complete Code to Use Review API with PHP

Here is the complete example code to create search query and make HTTP request to ReviewAPI to scrape the reviews data and get response data into JSON format.

<?php

$address = 'Madison Capital';
$apiKey = "YOUR_API_KEY";

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

$searchQuery = [
	'text' => $address,
	'apikey' => $apiKey,
];

curl_setopt($ch, CURLOPT_URL, 'https://app.geocodeapi.io/api/v1/search?' . http_build_query($searchQuery));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"Content-Type: application/json",
	"apikey: $apiKey",
));

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

$result = json_decode($response);

var_dump($result);

?>

Here is the sample Geocoding JSON data from Geocode API for the address “Madison Capital”

{
  "geocoding": {
    "version": "0.2",
    "attribution": "https://geocodeapi.io/attribution",
    "query": {
      "text": "Madison Capital",
      "size": 10,
      "layers": [
        "venue",
        "street",
        "country",
        "macroregion",
        "region",
        "county",
        "localadmin",
        "locality",
        "borough",
        "neighbourhood",
        "continent",
        "empire",
        "dependency",
        "macrocounty",
        "macrohood",
        "microhood",
        "disputed",
        "postalcode",
        "ocean",
        "marinearea"
      ],
      "private": false,
      "lang": {
        "name": "English",
        "iso6391": "en",
        "iso6393": "eng",
        "defaulted": true
      },
      "querySize": 20,
      "parser": "addressit",
      "parsed_text": {}
    },
    "warnings": [
      "performance optimization: excluding 'address' layer"
    ],
    "engine": {
      "name": "Pelias",
      "author": "Mapzen",
      "version": "1.0"
    },
    "timestamp": 1574492536205
  },
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -77.021517,
          38.913145
        ]
      },
      "properties": {
        "id": "way/67231845",
        "gid": "openstreetmap:venue:way/67231845",
        "layer": "venue",
        "source": "openstreetmap",
        "source_id": "way/67231845",
        "name": "Madison Saints Paradise South Capital City",
        "housenumber": "1713",
        "street": "7th Street Northwest",
        "confidence": 1,
        "match_type": "exact",
        "accuracy": "point",
        "country": "United States",
        "country_gid": "whosonfirst:country:85633793",
        "country_a": "USA",
        "region": "District of Columbia",
        "region_gid": "whosonfirst:region:85688741",
        "region_a": "DC",
        "locality": "Washington",
        "locality_gid": "whosonfirst:locality:85931779",
        "neighbourhood": "Shaw",
        "neighbourhood_gid": "whosonfirst:neighbourhood:85848053",
        "continent": "North America",
        "continent_gid": "whosonfirst:continent:102191575",
        "label": "Madison Saints Paradise South Capital City, Washington, DC, USA"
      },
      "bbox": [
        -77.0217809,
        38.913006,
        -77.0213335,
        38.9132801
      ]
    },  
}

Step6: Conclusion

Here in this tutorial you have learned how to integrate Geocode API with PHP to get the address information. You can also checkout the documentation for more details.

You may also like: