Aliyun.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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. * @package app\common\library\express\provider\driver
  20. */
  21. class Aliyun extends Driver
  22. {
  23. // API地址
  24. const API_URL = 'https://wdexpress.market.alicloudapi.com/gxali';
  25. /**
  26. * 查询物流轨迹
  27. * @param string $code 快递公司的编码
  28. * @param string $expressNo 查询的快递单号
  29. * @param array $extra 附加数据
  30. * @return array
  31. * @throws BaseException
  32. */
  33. public function query(string $code, string $expressNo, array $extra = []): array
  34. {
  35. // 授权参数
  36. $appCode = $this->options['appCode'];
  37. $headers = ["Authorization: APPCODE " . $appCode];
  38. // 物流查询参数
  39. $querys = ['n' => $expressNo, 't' => $code];
  40. // 请求API
  41. $result = $this->curlGet(self::API_URL, $headers, $querys);
  42. $data = helper::jsonDecode($result);
  43. // 记录日志
  44. log_record(['name' => '查询物流轨迹', 'provider' => 'aliyun', 'param' => $querys, 'result' => $data]);
  45. // 格式化返回的数据
  46. return $this->formatTraces($data['Traces']);
  47. }
  48. /**
  49. * 格式化返回的数据
  50. * @param array $source
  51. * @return array
  52. */
  53. private function formatTraces(array $source): array
  54. {
  55. return array_map(function ($item) {
  56. return ['time' => $item['AcceptTime'], 'context' => $item['AcceptStation']];
  57. }, array_reverse($source));
  58. }
  59. /**
  60. * curl请求指定url
  61. * @param string $url
  62. * @param array $headers
  63. * @param array $querys
  64. * @return bool|mixed|string
  65. * @throws BaseException
  66. */
  67. protected function curlGet(string $url, array $headers, array $querys)
  68. {
  69. $ch = curl_init();
  70. curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($querys));
  71. curl_setopt($ch, CURLOPT_HEADER, true);
  72. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  73. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  74. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  75. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  77. $result = curl_exec($ch);
  78. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  79. [$header, $body] = explode("\r\n\r\n", $result, 2);
  80. curl_close($ch);
  81. if ($httpCode == 200) {
  82. return $body;
  83. }
  84. $message = '参数名错误或其他错误';
  85. if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
  86. $message = '参数错误';
  87. } elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
  88. $message = 'AppCode错误';
  89. } elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
  90. $message = '请求的 Method、Path 或者环境错误';
  91. } elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
  92. $message = '服务未被授权(或URL和Path不正确)';
  93. } elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
  94. $message = '套餐包次数用完';
  95. } elseif ($httpCode == 500) {
  96. $message = 'API网关错误';
  97. } elseif ($httpCode == 0) {
  98. $message = 'URL错误';
  99. }
  100. throwError('阿里云物流查询API失败:' . $message);
  101. }
  102. }