使用阿里云 地址标准化 服务,解析物流面单信息-通用版
安装依赖包
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 协议》,转载必须注明作者和本文链接
推荐文章: