US Address Verification in PHP - Developer's quickstart guide
Welcome to the developer’s quickstart guide! This document will walk you through using the PHP SDK to access our US Address Verification product, but the same pattern applies to other Smarty products.
Before getting started, it’s important to note that most SDKs are hosted in Smarty’s official GitHub organization. The JavaScript SDK, Rust SDK, and some examples for TypeScript, React, and Vue can be found here. Also, if you’re working with another language, we’ve got support for that, too. These repositories also have examples of calling other Smarty APIs.
In-depth docs of other Smarty APIs can be found here.
Ready, set, let’s go!
Install
To get started with verifying US addresses, pull down Smarty’s PHP SDK to your development machine. The example below illustrates how to do this in composer.json. Find the latest version in the Smarty PHP SDK.
{ "require": { "smartystreets/phpsdk": "8.2.0" }}Quickstart
Import the Smarty SDK
Next, you’ll want to import the Smarty PHP SDK:
require_once(__DIR__ . '/vendor/autoload.php');use SmartyStreets\PhpSdk\BasicAuthCredentials;use SmartyStreets\PhpSdk\ClientBuilder;use SmartyStreets\PhpSdk\US_Street\Lookup;use SmartyStreets\PhpSdk\Batch;The us-street class provides the structures and endpoints to access Smarty’s US Address Verification and US Rooftop Geocoding products.
Create an API client
Build your client object with your Smarty credentials (secret keys).
$authId = getenv('SMARTY_AUTH_ID');$authToken = getenv('SMARTY_AUTH_TOKEN');$credentials = new BasicAuthCredentials($authId, $authToken);$client = (new ClientBuilder($credentials)) ->buildUsStreetApiClient();This client handles all HTTP complexities. The ClientBuilder provides a number of option methods, such as setting max retries, timeouts, base URL, etc. For authentication, we will use basic auth with the secret keys. We recommend storing your credentials in environment variables for security.
Create and batch lookups
The Smarty PHP SDK sends all address validation requests in batches for us-street-api and us-zipcode-api. To create a request, start by creating a batch of addresses to send for processing at Smarty. We've provided some sample addresses here, but feel free to load your own addresses however you'd like.
$addresses = ["1600 Pennsylvania Avenue Washington, DC","1600 Amphitheatre Parkway Mountain View, CA 94043"];$batch = new Batch();foreach ($addresses as $address) { $lookup = new Lookup(); $lookup->setStreet($address); $batch->add($lookup);}Here, we’ve created a batch by loading each address into a Lookup object. Next, we add those Lookups to a Batch. Typically, these addresses come from a file, database, etc., but we’ve left that part up to you, depending on your needs.
For each lookup, you can use a combination of at least:
street, city, and state
ORstreet and ZIP Code
ORstreet(full address in a single field)
As another example, Lookup could include street, city, state, and ZIP Code, even though breaking the address into these components is not needed to perform the lookup.
$address0 = new Lookup();$address0->setStreet("1600 amphitheatre parkway");$address0->setCity("mountain view");$address0->setState("CA");There are many other fields that allow you to customize your response, such as match type, candidates, inputID, etc. To explore all of these options, view the us-street-api documentation here.
Also note that each batch has a maximum size limit of 100; multiple concurrent batches can be sent.
Send batch
Once you have your Batch filled with Lookups, simply send the batch to get your validated results:
try { $client->sendBatch($batch);} catch (\Exception $ex) { echo($ex->getMessage());}To keep your integration reliable, we recommend handling any errors returned by the API. You can find a list of response codes for the us-street-api here.
Examine results
We’ve included a sample showing how you might loop through all results and print them, but at this point, you’re free to process the output however you like!
Keep in mind that each Lookup could have multiple results.
$lookups = $batch->getAllLookups();for ($i = 0; $i < $batch->size(); $i++) { $candidates = $lookups[$i]->getResult(); if (empty($candidates)) { echo("\nAddress " . $i . " is invalid.\n"); continue; } echo("\nAddress " . $i . " has at least one candidate. If the match parameter is set to STRICT, the address is valid. Otherwise, check the Analysis output fields to see if the address is valid.\n"); foreach ($candidates as $candidate) { $components = $candidate->getComponents(); $metadata = $candidate->getMetadata(); echo("\n\nCandidate " . $candidate->getCandidateIndex() . ":"); echo("\nDelivery line 1: " . $candidate->getDeliveryLine1()); echo("\nLast line: " . $candidate->getLastLine()); echo("\nZIP Code: " . $components->getZipcode() . "-" . $components->getPlus4Code()); echo("\nCounty: " . $metadata->getCountyName()); echo("\nLatitude: " . $metadata->getLatitude()); echo("\nLongitude: " . $metadata->getLongitude()); } echo("\n***********************************\n");}echo("\n");Documentation
Most of the SDKs are available in Smarty’s official GitHub organization, including the JavaScript SDK, Rust SDK, and repositories for TypeScript, React, and Vue. These repositories also have examples of calling other Smarty APIs.
In-depth docs for other Smarty APIs can be found here.
Was this helpful?