PHP code example of arraypress / google-address-validation
1. Go to this page and download the library: Download arraypress/google-address-validation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
arraypress / google-address-validation example snippets
use ArrayPress\Google\AddressValidation\Client;
// Initialize client with your API key
$client = new Client( 'your-google-api-key' );
// Validate an address
$result = $client->validate( '1600 Amphitheatre Parkway, Mountain View, CA' );
if ( ! is_wp_error( $result ) ) {
// Get validation score and rating
$score = $result->get_score();
$rating = $result->get_rating();
echo "Validation Score: {$score}/100 ({$rating})\n";
// Check if address is verified
if ( $result->is_verified() ) {
// Get standardized address
$address = $result->get_standardized_address();
echo "Formatted Address: {$address['formatted_address']}\n";
// Get coordinates
if ( $geocode = $result->get_geocode() ) {
echo "Latitude: {$geocode['latitude']}\n";
echo "Longitude: {$geocode['longitude']}\n";
}
}
}
$result = $client->validate( '1600 Amphitheatre Parkway, Mountain View, CA' );
if ( ! is_wp_error( $result ) ) {
// Get numerical score (0-100)
$score = $result->get_score();
// Get human-readable rating
$rating = $result->get_rating(); // Returns: Excellent, Good, Fair, or Poor
// Example score interpretation
switch (true) {
case $score >= 90:
echo "Excellent - Highly reliable address\n";
break;
case $score >= 75:
echo "Good - Reliable address with minor issues\n";
break;
case $score >= 50:
echo "Fair - Address may need verification\n";
break;
default:
echo "Poor - Address needs significant verification\n";
}
}
// Enable USPS validation for US addresses
$result = $client->validate(
'1600 Pennsylvania Avenue NW, Washington, DC 20500',
'US',
true // Enable USPS validation
);
if ( ! is_wp_error( $result ) ) {
$usps_data = $result->get_usps_data();
// Access USPS-specific data
}
$result = $client->validate( '123 Business Street, Anytown, USA' );
if ( ! is_wp_error( $result ) ) {
if ( $result->is_residential() ) {
echo "This is a residential address\n";
} elseif ( $result->is_po_box() ) {
echo "This is a PO Box\n";
} else {
echo "This is likely a commercial address\n";
}
}