-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathInternationalExample.php
More file actions
76 lines (60 loc) · 3.01 KB
/
Copy pathInternationalExample.php
File metadata and controls
76 lines (60 loc) · 3.01 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
<?php
require_once(__DIR__ . '/../src/BasicAuthCredentials.php');
// require_once(__DIR__ . '/../src/SharedCredentials.php');
require_once(__DIR__ . '/../src/ClientBuilder.php');
require_once(__DIR__ . '/../src/International_Street/Lookup.php');
require_once(__DIR__ . '/../src/International_Street/Client.php');
use SmartyStreets\PhpSdk\BasicAuthCredentials;
// use SmartyStreets\PhpSdk\SharedCredentials;
use SmartyStreets\PhpSdk\ClientBuilder;
use SmartyStreets\PhpSdk\International_Street\Lookup;
$lookupExample = new InternationalExample();
$lookupExample->run();
class InternationalExample {
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))
->buildInternationalStreetApiClient();
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/cloud/international-street-api
$lookup = new Lookup();
$lookup->setInputId("ID-8675309");
$lookup->setGeocode(true); // Must be expressly set to get latitude and longitude.
$lookup->setOrganization("John Doe");
$lookup->setAddress1("Rua Padre Antonio D'Angelo 121");
$lookup->setAddress2("Casa Verde");
$lookup->setLocality("Sao Paulo");
$lookup->setAdministrativeArea("SP");
$lookup->setCountry("Brazil");
$lookup->setPostalCode("02516-050");
// 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.
$this->displayResults($lookup);
}
catch (\Exception $ex) {
echo($ex->getMessage());
}
}
public function displayResults(Lookup $lookup) {
$firstCandidate = $lookup->getResult()[0];
echo("Address is " . $firstCandidate->getAnalysis()->getVerificationStatus());
echo("\nAddress precision: " . $firstCandidate->getAnalysis()->getAddressPrecision() . "\n");
echo("\nFirst Line: " . $firstCandidate->getAddress1());
echo("\nSecond Line: " . $firstCandidate->getAddress2());
echo("\nThird Line: " . $firstCandidate->getAddress3());
echo("\nFourth Line: " . $firstCandidate->getAddress4());
$metadata = $firstCandidate->getMetadata();
echo("\nAddress Format: " . $metadata->getAddressFormat());
echo("\nLatitude: " . $metadata->getLatitude());
echo("\nLongitude: " . $metadata->getLongitude());
}
}