文件
xionggeshop/extend/fast/ExpressBird.php

90 行
2.1 KiB
PHP

<?php
namespace fast;
use GuzzleHttp\Client;
/**
* 阿里云快递鸟Api
* Class ExpressBird
* @package fast
*/
class ExpressBird
{
/**
* 快递鸟api appCode
* @var string
*/
protected $appCode = '';
/**
* 接口域名
* @var string
*/
protected $host = 'https://alicloudmarket8002.kdniao.com';
/**
* GuzzleHttp客户端实例化
* @var \GuzzleHttp\Client
*/
protected $client;
public function __construct($appCode = '')
{
$this->appCode = trim((string) $appCode);
if ($this->appCode === '' && class_exists('\\addons\\shopro_aliyun_express\\library\\ExpressConfig')) {
$this->appCode = \addons\shopro_aliyun_express\library\ExpressConfig::getInstance()->getExpressBirdAppcode();
}
$this->client = new Client([
'http_errors' => false,
'exceptions' => false,
'base_uri' => $this->host,
]);
}
/**
* 查询快递轨迹
* @param string $logisticCode
* @param string $phone
* @return array
*/
public function queryTrack($logisticCode, $phone)
{
if ($this->appCode === '') {
return [
'Success' => false,
'Reason' => '请配置 ExpressBird APPCODE',
'Traces' => [],
];
}
$uri = '/api/track/8002';
$data = [
'CustomInfo' => 0000,
'LogisticCode' => $logisticCode
];
if (strpos($logisticCode, 'SF') !== false) {
$lastNumber = substr($phone, 7, 4);
$data['CustomerName'] = $lastNumber;
}
$res = $this->client->request('POST', $uri, [
'headers' => [
'Authorization' => 'APPCODE ' . $this->appCode,
'Content-Type' => 'application/json; charset=UTF-8'
],
'json' => $data
])->getBody()->getContents();
$res = json_decode($res, true);
if (!empty($res['Reason'])) {
$res['Traces'] = [];
}
return $res;
}
}