AliyunExpressService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\services;
  4. use app\model\api\Sys;
  5. /**
  6. * 阿里云物流查询服务
  7. */
  8. class AliyunExpressService
  9. {
  10. /**
  11. * 阿里云物流查询API
  12. * @var string
  13. */
  14. protected $api = 'https://wuliu.market.alicloudapi.com/kdi';
  15. /**
  16. * AppCode
  17. * @var string
  18. */
  19. protected $appCode;
  20. /**
  21. * 构造方法
  22. */
  23. public function __construct()
  24. {
  25. $sys = Sys::find(1);
  26. $this->appCode = $sys ? (string)($sys->system_express_app_code ?? '') : '';
  27. }
  28. /**
  29. * 查询物流信息
  30. * @param string $no 快递单号
  31. * @param string $type 快递公司编码(可选,阿里云接口可自动识别)
  32. * @return array|false
  33. */
  34. public function query(string $no, string $type = '')
  35. {
  36. try {
  37. // 断点1:appCode和请求参数
  38. @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] appCode=' . ($this->appCode ?: '(空)') . ', no=' . $no . ', type=' . $type . "\r\n", 8);
  39. if (empty($this->appCode)) {
  40. @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] appCode为空,直接返回false' . "\r\n", 8);
  41. return false;
  42. }
  43. $params = ['no' => $no];
  44. if (!empty($type)) {
  45. $params['type'] = $type;
  46. }
  47. $url = $this->api . '?' . http_build_query($params);
  48. // 断点2:请求URL
  49. @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] 请求URL: ' . $url . "\r\n", 8);
  50. $ch = curl_init();
  51. curl_setopt_array($ch, [
  52. CURLOPT_URL => $url,
  53. CURLOPT_RETURNTRANSFER => true,
  54. CURLOPT_HTTPHEADER => [
  55. 'Authorization: APPCODE ' . $this->appCode,
  56. ],
  57. CURLOPT_TIMEOUT => 30,
  58. CURLOPT_SSL_VERIFYPEER => false,
  59. ]);
  60. $result = curl_exec($ch);
  61. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  62. $curlError = curl_error($ch);
  63. curl_close($ch);
  64. // 断点3:HTTP状态码、curl错误、原始返回
  65. @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] httpCode=' . $httpCode . ', curlError=' . $curlError . ', result=' . $result . "\r\n", 8);
  66. if ($httpCode !== 200) {
  67. @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] httpCode非200,返回false' . "\r\n", 8);
  68. return false;
  69. }
  70. $decoded = json_decode($result, true);
  71. // 断点4:解析后的数据
  72. @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [ExpressService] 解析结果: ' . json_encode($decoded, JSON_UNESCAPED_UNICODE) . "\r\n", 8);
  73. return $decoded ?: false;
  74. } catch (\Exception $e) {
  75. @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);
  76. return false;
  77. }
  78. }
  79. /**
  80. * 物流状态码转中文
  81. * @param int $status
  82. * @return string
  83. */
  84. public function getStatusText(int $status): string
  85. {
  86. $map = [
  87. 0 => '在途',
  88. 1 => '揽收',
  89. 2 => '疑难',
  90. 3 => '签收',
  91. 4 => '退签',
  92. 5 => '派件',
  93. 6 => '退回',
  94. 7 => '转单',
  95. 10 => '待清关',
  96. 11 => '清关中',
  97. 12 => '已清关',
  98. 13 => '清关异常',
  99. 14 => '收件人拒签',
  100. ];
  101. return $map[$status] ?? '未知';
  102. }
  103. }