How to Validate a Street Address in PHP
2 min readMar 30, 2020
There are so many good reasons to use street address validation, especially when dealing with shipping. Every improper address leads to an unhappy customer or a lost package. It is much better to simply avoid the problem entirely. But how can we set this up without all of the hard work that is normally required? I’ll tell you: we are going to use an API.
To begin, we need to install our client, which requires running this command.
composer require cloudmersive/cloudmersive_validate_api_client
Now call this function to continue.
<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\AddressApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$input = new \Swagger\Client\Model\ValidateAddressRequest(); // \Swagger\Client\Model\ValidateAddressRequest | Input parse requesttry {$result = $apiInstance->addressValidateAddress($input);print_r($result);} catch (Exception $e) {echo 'Exception when calling AddressApi->addressValidateAddress: ', $e->getMessage(), PHP_EOL;}?>
With that done, any address can now be input and the API will validate it and provide coordinates, if valid. Done!