-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUsZIPCodeSingleLookupExample.php
66 lines (53 loc) · 2.41 KB
/
UsZIPCodeSingleLookupExample.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
<?php
require_once(dirname(dirname(__FILE__)) . '/src/ClientBuilder.php');
require_once(dirname(dirname(__FILE__)) . '/src/US_ZIPCode/Lookup.php');
require_once(dirname(dirname(__FILE__)) . '/src/US_ZIPCode/Result.php');
require_once(dirname(dirname(__FILE__)) . '/src/StaticCredentials.php');
require_once(dirname(dirname(__FILE__)) . '/src/SharedCredentials.php');
use SmartyStreets\PhpSdk\Exceptions\SmartyException;
use SmartyStreets\PhpSdk\StaticCredentials;
use SmartyStreets\PhpSdk\US_ZIPCode\Lookup;
use SmartyStreets\PhpSdk\ClientBuilder;
$lookupExample = new UsZIPCodeSingleLookupExample();
$lookupExample->run();
class UsZIPCodeSingleLookupExample {
public function run() {
// $authId = 'Your SmartyStreets Auth ID here';
// $authToken = 'Your SmartyStreets Auth Token here';
// We recommend storing your secret keys in environment variables instead---it's safer!
$authId = getenv('SMARTY_AUTH_ID');
$authToken = getenv('SMARTY_AUTH_TOKEN');
$staticCredentials = new StaticCredentials($authId, $authToken);
$client = (new ClientBuilder($staticCredentials))->buildUsZIPCodeApiClient();
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/cloud/us-zipcode-api
$lookup = new Lookup();
$lookup->setInputId("dfc33cb6-829e-4fea-aa1b-b6d6580f0817"); // Optional ID from you system
$lookup->setCity("Mountain View");
$lookup->setState("California");
// Uncomment the below line to add a custom parameter to the API call
// $lookup->addCustomParameter("parameter", "value");
try {
$client->sendLookup($lookup);
$this->displayResults($lookup);
}
catch (\Exception $ex) {
echo($ex->getMessage());
}
}
public function displayResults(Lookup $lookup) {
$result = $lookup->getResult();
$zipCodes = $result->getZIPCodes();
$cities = $result->getCities();
foreach ($cities as $city) {
echo("\nCity: " . $city->getCity());
echo("\nState: " . $city->getState());
echo("\nMailable City: " . json_encode($city->getMailableCity()));
}
foreach ($zipCodes as $zip) {
echo("\n\nZIP Code: " . $zip->getZIPCode());
echo("\nLatitude: " . $zip->getLatitude());
echo("\nLongitude: " . $zip->getLongitude());
}
}
}