| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- declare (strict_types = 1);
- namespace app\services;
- use app\model\api\Sys;
- /**
- * 阿里云物流查询服务
- */
- class AliyunExpressService
- {
- /**
- * 阿里云物流查询API
- * @var string
- */
- protected $api = 'https://wuliu.market.alicloudapi.com/kdi';
- /**
- * AppCode
- * @var string
- */
- protected $appCode;
- /**
- * 构造方法
- */
- public function __construct()
- {
- $sys = Sys::find(1);
- $this->appCode = $sys ? (string)($sys->system_express_app_code ?? '') : '';
- }
- /**
- * 查询物流信息
- * @param string $no 快递单号
- * @param string $type 快递公司编码(可选,阿里云接口可自动识别)
- * @return array|false
- */
- public function query(string $no, string $type = '')
- {
- return $this->appCode;
- if (empty($this->appCode)) {
- return false;
- }
- $params = ['no' => $no];
- if (!empty($type)) {
- $params['type'] = $type;
- }
- $url = $this->api . '?' . http_build_query($params);
- $ch = curl_init();
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HTTPHEADER => [
- 'Authorization: APPCODE ' . $this->appCode,
- ],
- CURLOPT_TIMEOUT => 30,
- CURLOPT_SSL_VERIFYPEER => false,
- ]);
- $result = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- if ($httpCode !== 200) {
- return false;
- }
- return json_decode($result, true) ?: false;
- }
- /**
- * 物流状态码转中文
- * @param int $status
- * @return string
- */
- public function getStatusText(int $status): string
- {
- $map = [
- 0 => '在途',
- 1 => '揽收',
- 2 => '疑难',
- 3 => '签收',
- 4 => '退签',
- 5 => '派件',
- 6 => '退回',
- 7 => '转单',
- 10 => '待清关',
- 11 => '清关中',
- 12 => '已清关',
- 13 => '清关异常',
- 14 => '收件人拒签',
- ];
- return $map[$status] ?? '未知';
- }
- }
|