How to Geocode a Street Address in PHP

2 min readMay 17, 2024

Incorporating geolocation capabilities into our application is a great way to expand and improve our user experience.

By geolocating street addresses from user-supplied input, we can easily enable our application to find local services, calculate distances from the user’s location to another location, and generally display relevant local information in the user’s session.

To our own benefit, geolocation can help us stay compliant with various regulatory requirements, and it can prove useful when we analyze information about our application’s user base.

Conveniently, using the PHP code examples below, we can take advantage of a free street address Geolocation API. With a standard address input like the following example:

"StreetAddress": "4695 Chabot Drive",
"City": "Pleasanton,",
"StateOrProvince": "California",
"PostalCode": "94588",
"CountryFullName": "United States",
"CountryCode": "string"

We’ll get latitude and longitude information associated with that address. We’ll also get an address validation check before geolocation information is returned:

"ValidAddress": true,
"Latitude": 37.6890016,
"Longitude": -121.901832

Before we copy code examples to call this API, we’ll first need to grab a free Cloudmersive API key. This will allow us to make up to 800 API calls per month with zero commitments (our total will simply reset the following month once we reach our limit).

To install the PHP client with Composer, we can execute the following command:

composer require cloudmersive/cloudmersive_validate_api_client

Then we can copy the below examples to call the geolocation function. We can paste our API key in the ‘YOUR_API_KEY’ placeholder string:

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(),
$input = new \Swagger\Client\Model\ValidateAddressRequest(); // \Swagger\Client\Model\ValidateAddressRequest | Input parse request

try {
$result = $apiInstance->addressGeocode($input);
} catch (Exception $e) {
echo 'Exception when calling AddressApi->addressGeocode: ', $e->getMessage(), PHP_EOL;

Just like that, we can now easily geolocate street addresses using a few lines of PHP code.




There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.