-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathInternationalPostalCodeExample.php
More file actions
79 lines (64 loc) · 2.86 KB
/
Copy pathInternationalPostalCodeExample.php
File metadata and controls
79 lines (64 loc) · 2.86 KB
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
<?php
require_once(__DIR__ . '/../src/BasicAuthCredentials.php');
// require_once(__DIR__ . '/../src/SharedCredentials.php');
require_once(__DIR__ . '/../src/ClientBuilder.php');
require_once(__DIR__ . '/../src/International_Postal_Code/Lookup.php');
require_once(__DIR__ . '/../src/International_Postal_Code/Client.php');
use SmartyStreets\PhpSdk\BasicAuthCredentials;
// use SmartyStreets\PhpSdk\SharedCredentials;
use SmartyStreets\PhpSdk\ClientBuilder;
use SmartyStreets\PhpSdk\International_Postal_Code\Lookup;
$lookupExample = new InternationalPostalCodeExample();
$lookupExample->run();
class InternationalPostalCodeExample {
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');
// For client-side requests (browser/mobile), use SharedCredentials:
// $credentials = new SharedCredentials($key, $hostname);
$credentials = new BasicAuthCredentials($authId, $authToken);
$client = (new ClientBuilder($credentials))
->buildInternationalPostalCodeApiClient();
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/cloud/international-postal-code-api
$lookup = new Lookup();
$lookup->setInputId("ID-8675309");
$lookup->setLocality("Sao Paulo");
$lookup->setAdministrativeArea("SP");
$lookup->setCountry("Brazil");
$lookup->setPostalCode("02516");
try {
$client->sendLookup($lookup); // The candidates are also stored in the lookup's 'result' field.
$this->displayResults($lookup);
}
catch (\Exception $ex) {
echo($ex->getMessage());
}
}
public function displayResults(Lookup $lookup) {
$results = $lookup->getResult();
echo("Results:\n");
echo("\n");
foreach ($results as $c => $candidate) {
echo("Candidate: " . $c . "\n");
$this->display($candidate->getInputId());
$this->display($candidate->getCountryIso3());
$this->display($candidate->getLocality());
$this->display($candidate->getDependentLocality());
$this->display($candidate->getDoubleDependentLocality());
$this->display($candidate->getSubAdministrativeArea());
$this->display($candidate->getAdministrativeArea());
$this->display($candidate->getSuperAdministrativeArea());
$this->display($candidate->getPostalCode());
echo("\n");
}
}
private function display($value) {
if ($value != null && strlen($value) > 0) {
echo(" " . $value . "\n");
}
}
}