-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathInternationalAutocompleteExample.php
57 lines (45 loc) · 2.37 KB
/
InternationalAutocompleteExample.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
<?php
require_once(dirname(dirname(__FILE__)) . '/src/StaticCredentials.php');
require_once(dirname(dirname(__FILE__)) . '/src/ClientBuilder.php');
require_once(dirname(dirname(__FILE__)) . '/src/International_Autocomplete/Lookup.php');
require_once(dirname(dirname(__FILE__)) . '/src/International_Autocomplete/Client.php');
use SmartyStreets\PhpSdk\StaticCredentials;
use SmartyStreets\PhpSdk\ClientBuilder;
use SmartyStreets\PhpSdk\International_Autocomplete\Lookup;
$lookupExample = new InternationalAutocompleteExample();
$lookupExample->run();
class InternationalAutocompleteExample {
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);
// 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($staticCredentials)) ->withLicenses(["international-autocomplete-v2-cloud"])
->buildInternationalAutocompleteApiClient();
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/cloud/international-street-api
$lookup = new Lookup("Louis");
$lookup->setCountry("FRA");
$lookup->setLocality("Paris");
// Uncomment the below line to add a custom parameter to the API call
// $lookup->addCustomParameter("parameter", "value");
try {
$client->sendLookup($lookup); // The candidates are also stored in the lookup's 'result' field.
foreach ($lookup->getResult() as $candidate) {
if ($candidate->getStreet() != null) {
echo($candidate->getStreet() . " " . $candidate->getLocality() . " " . $candidate->getCountryISO3() . "\n");
} else {
echo($candidate->getEntries() . " " . $candidate->getAddressText() . " " . $candidate->getAddressID() . "\n");
}
};
}
catch (\Exception $ex) {
echo($ex->getMessage());
}
}
}