PHP Classes

PHP Address Autocomplete and Verification: Authenticate and autocomplete street addresses

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 428 All time: 6,330 This week: 206Up
Version License PHP version Categories
streetlayer 1.0.1GNU General Publi...5PHP 5, Web services, Geography
Description 

Author

This class can authenticate and autocomplete street addresses.

It sends HTTP requests to the streetLayer API Web server to perform several functions:

1. Authenticate an address to verify that it really exists

2. Provide autocomplete text options to an address partially entered

3. Reverse search to addresses close to a geographic location

Innovation Award
PHP Programming Innovation award nominee
February 2017
Number 2
Many sites need to request users to enter their postal addresses, like for instance in ecommerce sites. This may be a tedious task for the user, especially those that do not like to type on mobile devices.

Often users give up registering or buying a product when they need to fully enter their postal addresses on forms. So bureaucratic form filling may cause loss of customers.

This package provides a solution to minimize the pain of the users that need to enter postal addresses in forms. It provides means to autocomplete an address partially entered by the user in a Web form.

When used in conjunction with AJAX requests, it can autocomplete the address while the user is typing in on the form without having to reload the current page.

The possibility to validate if an address is correct is also very useful as it may avoid problems of delivering goods to addresses that are incorrect.

Manuel Lemos
Picture of Dave Smith
  Performance   Level  
Name: Dave Smith is available for providing paid consulting. Contact Dave Smith .
Classes: 51 packages by
Country: United States United States
Age: 59
All time rank: 608 in United States United States
Week rank: 19 Up2 in United States United States Up
Innovation award
Innovation award
Nominee: 32x

Winner: 7x

Recommendations

Validate US address using USPS API
I would like to validate US postal addresses using USPS API

Example

<?php
/*
example usage
streetLayer ver 1.0

You must get an API key from https://streetlayer.com/product
and enter it in the streetlayer.class.php file

For more documentation, visit https://streetlayer.com/documentation
*/

//turning off low level notices
error_reporting(E_ALL ^ E_NOTICE);

//set your plan to diplay features associated with it.
//plans are free, basic, pro and enterprise
$plan = 'free';

//instantiate the class
include('streetlayer.class.php');
$streetLayer = new streetLayer();

//set our endpoint
//defaults to the validate endpoint, but we will set it just to be safe
$streetLayer->setEndPoint('validate');

/*
specify the address to validate
at a minimum, you must provide the required parameters
and one additional one.
valid parameters are:

address1 (required)
country_code (required) - can be the 2 or 3 digit country code
locality - you may also use 'city' as an alias
region - you may also use 'state' as an alias
postal_code - you may also use 'zip', 'zipcode', 'zip_code', 'postal_code'
county
*/

$streetLayer->setParam('address1','725 5th Ave');
$streetLayer->setParam('locality','New York');
$streetLayer->setParam('region','NY');
$streetLayer->setParam('postal_code','10022');
$streetLayer->setParam('country_code','US');

//get the response from the api
$streetLayer->getResponse();

//the reponse property will contain the response returned from the api
echo '<h4>The validation request returned</h4>';
$success = ( $streetLayer->response->success === true ) ? 'Yes' : 'No';
echo
'Success: '. $success.'<br>';
echo
'Validated: '.$streetLayer->response->validation_status.'<br>';
echo
'The formatted address is:<br><br>';

foreach(
$streetLayer->response->formatted_address as $addressLine ){
    echo
$addressLine.'<br>';
}

echo
'<br>GPS Coordinate are<br><br>';

foreach(
$streetLayer->response->coordinates as $key=>$value ){
    echo
$key.': '.$value.'<br>';
}

/*
You can also use a second method to validate an address
valid parameter are:

text (required)
country_code (required)

$streetLayer->setParam('text','725 5th Ave New York, NY 10022');
$streetLayer->setParam('country_code','US');
$streetLayer->getResponse();

*/

//change our endpoint to use the address autocomplete
$streetLayer->setEndPoint('autocomplete');

/*
specify the address to autocomplete
at a minimum, you must provide the required parameters
pro and enterprise plans allow for the postal code to
return better results
valid parameters are:

text (required)
country_code (required)
postal_code - you may also use 'zip', 'zipcode', 'zip_code', 'postal_code'
*/

$streetLayer->setParam('text','725 5th Ave New York');
$streetLayer->setParam('country_code','US');
if(
$plan == 'pro' or $plan == 'enterprise' ){
   
$streetLayer->setParam('postal_code','10022');
}

//get the response from the api
$streetLayer->getResponse();

echo
'<h4>The autocomplete request returned</h4>';
$success = ( $streetLayer->response->success == true ) ? 'Yes' : 'No';
echo
'Success: '. $success.'<br>';
if(
$success == 'Yes' ){
    echo
'The formatted address choices are:<br><br>';

   
$count = 1;

    foreach(
$streetLayer->response->results as $address ){
        echo
$count.')<br>';

        foreach(
$address->formatted_address as $addressLine ){
            if( empty(
$addressLine) ){continue;}
            echo
$addressLine.'<br>';
        }

        echo
'<br>GPS Coordinate are<br><br>';

        foreach(
$address->coordinates as $key=>$value ){
            if( empty(
$value) ){continue;}
            echo
$key.': '.$value.'<br>';
        }

        echo
'<br>';
       
$count++;

    }

}


//change our endpoint to use the reverse geocode lookup
$streetLayer->setEndPoint('reverse');

/*
specify the coordinates
valid parameters are:

latitude (required) - you may also use 'lat'
lognitude (required) - you may also use 'lon' or 'long'
*/

$streetLayer->setParam('lat',40.762342);
$streetLayer->setParam('lon',-73.973992);

//get the response from the api
$streetLayer->getResponse();

echo
'<h4>The reverse geocode lookup returned</h4>';
$success = ( $streetLayer->response->success == true ) ? 'Yes' : 'No';
echo
'Success: '. $success.'<br>';
if(
$success == 'Yes' ){
    echo
'The formatted address choices are:<br><br>';

   
$count = 1;

    foreach(
$streetLayer->response->results as $address ){
        echo
$count.')<br>';

        foreach(
$address->formatted_address as $addressLine ){
            if( empty(
$addressLine) ){continue;}
            echo
$addressLine.'<br>';
        }

        echo
'<br>GPS Coordinate are<br><br>';

        foreach(
$address->coordinates as $key=>$value ){
            if( empty(
$value) ){continue;}
            echo
$key.': '.$value.'<br>';
        }

        echo
'<br>';
       
$count++;

    }

}
?>


  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example Usage
Accessible without login Plain text file license.txt Lic. License
Plain text file streetlayer.class.php Class Main Class

 Version Control Unique User Downloads Download Rankings  
 0%
Total:428
This week:0
All time:6,330
This week:206Up