How to Validate an Address in PHP

Cloudmersive
1 min readFeb 15, 2021

--

Do you collect and store user addresses for shipping and billing purposes? If the answer is yes, then you know the importance of ensuring that the gathered location information is accurate. The following API will allow you to validate street addresses, including all major international addresses, by parsing the input data request in PHP.

Our first step in the validation process is to install the SDK by running this command:

composer require cloudmersive/cloudmersive_validate_api_client

Next, we will input both our API key and our parse request (including street address, city, state/province, postal code, country name, and country code) and call the function with the following code:

<?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->addressValidateAddress($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling AddressApi->addressValidateAddress: ', $e->getMessage(), PHP_EOL;
}
?>

The finished result will verify the validity of the location; if the address is valid, longitude and latitude will be indicated as well.

--

--

Cloudmersive

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