How to Geocode a Street Address into Latitude, Longitude using PHP
Implementing a geocoding API into your application makes it possible to quickly translate text address data into plottable latitude and longitude values. It’s a hassle to create this type of service from scratch — thankfully, you can easily take advantage of our free-to-use Geocoding API using the ready-to-run PHP code examples provided below.
Just run this command to install the SDK:
composer require cloudmersive/cloudmersive_validate_api_client
Then use the remaining code examples to structure your API call, and include an API key with your request (you can get one for free by registering a free account here):
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\AddressApi(
new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\ValidateAddressRequest(); // \Swagger\Client\Model\ValidateAddressRequest | Input parse request
try {
$result = $apiInstance->addressGeocode($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling AddressApi->addressGeocode: ', $e->getMessage(), PHP_EOL;
}
?>
That’s all there is to it — no more code required! You can follow this example JSON model to structure your input parse request:
{
"StreetAddress": "string",
"City": "string",
"StateOrProvince": "string",
"PostalCode": "string",
"CountryFullName": "string",
"CountryCode": "string"
}