-
Notifications
You must be signed in to change notification settings - Fork 15
/
USAutocompleteProExample.php
82 lines (67 loc) · 2.86 KB
/
USAutocompleteProExample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
require_once(dirname(dirname(__FILE__)) . '/src/SharedCredentials.php');
require_once(dirname(dirname(__FILE__)) . '/src/StaticCredentials.php');
require_once(dirname(dirname(__FILE__)) . '/src/ClientBuilder.php');
require_once(dirname(dirname(__FILE__)) . '/src/US_Autocomplete_Pro/Lookup.php');
require_once(dirname(dirname(__FILE__)) . '/src/US_Autocomplete_Pro/Client.php');
use SmartyStreets\PhpSdk\SharedCredentials;
use SmartyStreets\PhpSdk\StaticCredentials;
use SmartyStreets\PhpSdk\ClientBuilder;
use SmartyStreets\PhpSdk\US_Autocomplete_Pro\Lookup;
use SmartyStreets\PhpSdk\US_Autocomplete_Pro\Suggestion;
$lookupExample = new USAutocompleteProExample();
$lookupExample->run();
class USAutocompleteProExample
{
public function run()
{
// We recommend storing your secret keys in environment variables---it's safer!
// $key = getenv('SMARTY_WEBSITE_KEY');
// $hostname = getenv('SMARTY_WEBSITE_DOMAIN');
// $credentials = new SharedCredentials($key, $hostname);
$id = getenv('SMARTY_AUTH_ID');
$token = getenv('SMARTY_AUTH_TOKEN');
$credentials = new StaticCredentials($id, $token);
// The appropriate license values to be used for your subscriptions
// can be found on the Subscriptions page the account dashboard.
// https://www.smartystreets.com/docs/cloud/licensing
$client = (new ClientBuilder($credentials)) ->withLicenses(["us-autocomplete-pro-cloud"])
->buildUSAutocompleteProApiClient();
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/cloud/us-autocomplete-api
$lookup = new Lookup("1042 W Center");
try {
$client->sendLookup($lookup);
$this->displayResultsNoFilter($lookup);
$lookup->addCityFilter("Denver,Aurora,CO");
$lookup->addCityFilter("Orem,UT");
// $lookup->addPreferState("CO");
$lookup->setPreferRatio(3);
$lookup->setMaxResults(5);
$lookup->setSource("all");
$client->sendLookup($lookup);
$this->displayResultsFilter($lookup);
}
catch (\Exception $ex) {
echo($ex->getMessage());
}
}
private function displayResultsNoFilter(Lookup $lookup)
{
echo("*** Result with no filter ***");
echo("\n");
foreach ($lookup->getResult() as $suggestion)
$this->printResults($suggestion);
}
private function displayResultsFilter(Lookup $lookup)
{
echo("\n");
echo("*** Result with some filters ***\n");
foreach($lookup->getResult() as $suggestion)
$this->printResults($suggestion);
}
private function printResults(Suggestion $suggestion)
{
echo($suggestion->getStreetLine() . " " . $suggestion->getCity() . ", " . $suggestion->getState() . "\n");
}
}