jdavidbakr/address-verification

一个使用https://www.intelligentsearch.com/ API进行地址验证的服务

2.1.2 2020-09-11 15:22 UTC

This package is auto-updated.

Last update: 2024-09-11 02:32:39 UTC


README

Latest Version on Packagist Software License Total Downloads

从1.x版本升级

以下是在1.x和2.0版本之间发生的变更

  • 配置信息现在不再添加到config/services.php文件中,而是有自己的文件
  • 现在,该包会抛出异常和事件,而不是期望您进行处理。

如何使用

一个使用intelligentsearch.com验证邮寄地址的Laravel服务。

此类通过API将地址发送到https://www.intelligentsearch.com/,并更新地址为最相关的结果。它构建在Laravel内部运行。

注意,在其当前状态下,它不返回多个结果。

安装

composer require jdavidbakr/address-verification

在Laravel中自动发现该包。

将以下部分添加到config/services.php

    'intelligentsearch'=>[
        'username'=>env('INTELLIGENTSEARCH_USERNAME'),
        'password'=>env('INTELLIGENTSEARCH_PASSWORD'),
        'cache_time'=>90, // in days.  Set to 0 to have no cache.
    ],

现在将有一个位于config/address-verification.php的文件来设置您的intelligentsearch.com用户名和密码。

使用方法

创建一个类型为\jdavidbakr\AddressVerification\AddressRequest的对象,并填写适当的值。然后使用创建的请求调用\jdavidbakr\AddressVerification\AddressVerificationService::Verify($request)。您将收到一个类型为\jdavidbakr\AddressVerification\AddressVerificationResponse的对象。

$request = new \jdavidbakr\AddressVerification\AddressRequest;
$request->delivery_line_1 = '1600 Pennsylvania Ave NW';
$request->city_state_zip = 'Washington DC 20500';
try {
    $result = \jdavidbakr\AddressVerification\AddressVerificationService::Verify($request);
    // Alternatively use the facade:
    $result = AddressVerification::Verify($request);
} catch(\jdavidbakr\AddressVerification\VerificationFailedException $e) {
    // Handle what to do if the verification failed
}

请求默认为McRy的ca_codes,它返回混合大小写并启用不匹配地址的街道地址解析。

如果成功,您将收到一个AddressResponse对象。您可能会在响应中使用的属性包括

  • DeliveryLine1
  • City
  • State
  • ZipAddon

异常

以下异常可能会被抛出(所有名称空间都来自包)

  • VerificationFailedException - 您应该关注此异常并决定如何处理它。基本上,这意味着您没有从响应中获取任何数据。
  • MissingIntelligentSearchCredentialsException - 如果您未包含凭证则触发。

事件

  • AddressVerificationCompleted - 这是一个在每次地址验证完成后都会触发的事件。使用它来检查SearchesLeft属性,如果您想发送一个通知表示它正在变低。它在其有效负载中接收AddressResponse
public function handle($event)
{
    $searchesLeft = $event->response->SearchesLeft;
    if($searchesLeft < 1000) {
        Bugsnag::notifyError('LowIntelligentSearchQueries', 'IntelligentSearch queries are low', function($report) use($searchesLeft) {
            $report->setSeverity('warning');
            $report->setMetaData([
                'remaining'=>$searchesLeft
            ]);
        });
    }
}

查看IntelligentSearch文档以了解所有返回字段以及请求属性的具体信息。