PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Dave Smith   PHP Address Autocomplete and Verification   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example Usage
Class: PHP Address Autocomplete and Verification
Authenticate and autocomplete street addresses
Author: By
Last change:
Date: 7 years ago
Size: 4,945 bytes
 

Contents

Class file image Download
<?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++;

    }

}
?>