Aliyun.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\express\provider\driver;
  13. use app\common\library\helper;
  14. use app\common\library\express\provider\Driver;
  15. use cores\exception\BaseException;
  16. /**
  17. * 阿里云物流查询驱动
  18. * Class Aliyun
  19. * 接口文档: https://market.aliyun.com/products/57126001/cmapi023201.html
  20. * @package app\common\library\express\provider\driver
  21. */
  22. class Aliyun extends Driver
  23. {
  24. // API地址
  25. const API_URL = 'https://wdexpress.market.alicloudapi.com/gxali';
  26. /**
  27. * 查询物流轨迹
  28. * @param string $code 快递公司的编码
  29. * @param string $expressNo 查询的快递单号
  30. * @param array $extra 附加数据
  31. * @return array
  32. * @throws BaseException
  33. */
  34. public function query(string $code, string $expressNo, array $extra = []): array
  35. {
  36. // 授权参数
  37. $appCode = $this->options['appCode'];
  38. $headers = ["Authorization: APPCODE " . $appCode];
  39. // 查询顺丰和中通时 物流单号需要加上手机尾号
  40. if (\in_array($code, ['SF', 'ZTO'])) {
  41. $lastPhoneNumber = \mb_substr($extra['phone'], -4);
  42. $expressNo = "{$expressNo}:$lastPhoneNumber";
  43. }
  44. // 物流查询参数
  45. $querys = ['n' => $expressNo, 't' => $code];
  46. // 请求API
  47. $result = $this->curlGet(self::API_URL, $headers, $querys);
  48. $data = helper::jsonDecode($result);
  49. // 记录日志
  50. log_record(['name' => '查询物流轨迹', 'provider' => 'aliyun', 'param' => $querys, 'result' => $data]);
  51. // 错误信息
  52. if ($data['State'] <= 0 || !$data['Success']) {
  53. throwError('阿里云物流查询API失败:' . $data['Reason']);
  54. }
  55. // 格式化返回的数据
  56. return $this->formatTraces($data['Traces']);
  57. }
  58. /**
  59. * 格式化返回的数据
  60. * @param array $source
  61. * @return array
  62. */
  63. private function formatTraces(array $source): array
  64. {
  65. return \array_map(fn($item) => ['time' => $item['AcceptTime'], 'context' => $item['AcceptStation']]
  66. , array_reverse($source));
  67. }
  68. /**
  69. * curl请求指定url
  70. * @param string $url
  71. * @param array $headers
  72. * @param array $querys
  73. * @return bool|mixed|string
  74. * @throws BaseException
  75. */
  76. protected function curlGet(string $url, array $headers, array $querys)
  77. {
  78. $ch = curl_init();
  79. curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($querys));
  80. curl_setopt($ch, CURLOPT_HEADER, true);
  81. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  82. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  83. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  84. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  85. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  86. $result = curl_exec($ch);
  87. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  88. [$header, $body] = explode("\r\n\r\n", $result, 2);
  89. curl_close($ch);
  90. if ($httpCode == 200) {
  91. return $body;
  92. }
  93. $message = '参数名错误或其他错误';
  94. if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
  95. $message = '参数错误';
  96. } elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
  97. $message = 'AppCode错误';
  98. } elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
  99. $message = '请求的 Method、Path 或者环境错误';
  100. } elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
  101. $message = '服务未被授权(或URL和Path不正确)';
  102. } elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
  103. $message = '套餐包次数用完';
  104. } elseif ($httpCode == 500) {
  105. $message = 'API网关错误';
  106. } elseif ($httpCode == 0) {
  107. $message = 'URL错误';
  108. }
  109. throwError('阿里云物流查询API失败:' . $message);
  110. }
  111. }