forked from smartystreets/smartystreets-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSExtractExample.php
More file actions
75 lines (62 loc) · 2.81 KB
/
USExtractExample.php
File metadata and controls
75 lines (62 loc) · 2.81 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
<?php
require_once(__DIR__ . '/../src/ArrayUtil.php');
require_once(__DIR__ . '/../src/StaticCredentials.php');
require_once(__DIR__ . '/../src/ClientBuilder.php');
require_once(__DIR__ . '/../src/US_Extract/Lookup.php');
require_once(__DIR__ . '/../src/US_Extract/Client.php');
use SmartyStreets\PhpSdk\StaticCredentials;
use SmartyStreets\PhpSdk\ClientBuilder;
use SmartyStreets\PhpSdk\US_Extract\Lookup;
use SmartyStreets\PhpSdk\ArrayUtil;
$lookupExample = new USExtractExample();
$lookupExample->run();
class USExtractExample {
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))->buildUSExtractApiClient();
$text = "Here is some text.\r\nMy address is 3785 Las Vegs Av." .
"\r\nLos Vegas, Nevada." .
"\r\nMeet me at 1 Rosedale Baltimore Maryland, not at 123 Phony Street, Boise Idaho.";
// Documentation regarding input fields can be found at:
// https://smartystreets.com/docs/cloud/us-extract-api
$lookup = new Lookup($text);
$lookup->isAggressive();
$lookup->setAddressesHaveLineBreaks(false);
$lookup->setAddressesPerLine(2);
// 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();
$metadata = $result->getMetadata();
print('Found ' . $metadata->getAddressCount() . " addresses.\n");
print($metadata->getVerifiedCount() . " of them were valid.\n\n");
$addresses = $result->getAddresses();
print("Addresses: \n**********************\n");
foreach ($addresses as $address) {
print("\n\"" . $address->getText() . "\"\n");
print("\nVerified? " . ArrayUtil::getStringValueOfBoolean($address->isVerified()));
if (count($address->getCandidates()) > 0) {
print("\nMatches:");
foreach ($address->getCandidates() as $candidate) {
print("\n" . $candidate->getDeliveryLine1());
print("\n" . $candidate->getLastLine() . "\n");
}
} else print("\n");
print("**********************\n");
}
}
}