| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?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 = '')
- {
- try {
- // 断点1:appCode和请求参数
- // @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] appCode=' . ($this->appCode ?: '(空)') . ', no=' . $no . ', type=' . $type . "\r\n", 8);
- if (empty($this->appCode)) {
- // @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] appCode为空,直接返回false' . "\r\n", 8);
- return false;
- }
- $params = ['no' => $no];
- if (!empty($type)) {
- $params['type'] = $type;
- }
- $url = $this->api . '?' . http_build_query($params);
- // 断点2:请求URL
- // @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] 请求URL: ' . $url . "\r\n", 8);
- $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);
- $curlError = curl_error($ch);
- curl_close($ch);
- // 断点3:HTTP状态码、curl错误、原始返回
- // @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] httpCode=' . $httpCode . ', curlError=' . $curlError . ', result=' . $result . "\r\n", 8);
- if ($httpCode !== 200) {
- @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] httpCode非200,返回false' . "\r\n", 8);
- return false;
- }
- $decoded = json_decode($result, true);
- // 断点4:解析后的数据
- @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] 解析结果: ' . json_encode($decoded, JSON_UNESCAPED_UNICODE) . "\r\n", 8);
- return $decoded ?: false;
- } catch (\Exception $e) {
- @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService Error] ' . $e->getMessage() . ' File: ' . $e->getFile() . ' Line: ' . $e->getLine() . "\r\n", 8);
- return 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] ?? '未知';
- }
- }
|