使用阿里云 地址标准化 服务,解析物流面单信息-通用版

安装依赖包

composer require alibabacloud/client

解析物流面单信息

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class Addresspurification {

    // 行政区划识别
    public function getAddressDivisionCode($address){
        AlibabaCloud::accessKeyClient('<your-access-key-id>', '<your-access-key-secret>')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('address-purification')
                ->version('2019-11-18')
                ->action('GetAddressDivisionCode')
                ->host('address-purification.cn-hangzhou.aliyuncs.com')
                ->options([
                    'query' => [
                        'AppKey' => "<your-app-key>",
                        'ServiceCode' => "addrp",
                        'Text' => $address,
                    ],
                ])
                ->request();

            $resultArr = $result->toArray();
            $addressArr = json_decode($resultArr["Data"],true);

            $addressStr = $addressArr['division_info']['division_name'];
            $addressStrImplode = explode(';',$addressStr);

        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

    // 物流面单信息提取
    public function extractExpress($address){
        AlibabaCloud::accessKeyClient('<your-access-key-id>', '<your-access-key-secret>')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('address-purification')
                ->version('2019-11-18')
                ->action('GetAddressDivisionCode')
                ->host('address-purification.cn-hangzhou.aliyuncs.com')
                ->options([
                    'query' => [
                        'AppKey' => "<your-app-key>",
                        'ServiceCode' => "addrp",
                        'Text' => $address,
                    ],
                ])
                ->request();
            $addressArr = json_decode($result["Data"],true);
            $addressStr = $addressArr['express_extract'];
            return $addressStr;
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

}
本作品采用《CC 协议》,转载必须注明作者和本文链接
www.haowuliaoa.com
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!