Aliyun.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * @return array
  30. * @throws BaseException
  31. */
  32. public function query(string $code, string $expressNo): array
  33. {
  34. // 授权参数
  35. $appCode = $this->options['appCode'];
  36. $headers = ["Authorization: APPCODE " . $appCode];
  37. // 物流查询参数
  38. $querys = ['n' => $expressNo, 't' => $code];
  39. // 请求API
  40. $result = $this->curlGet(self::API_URL, $headers, $querys);
  41. $data = helper::jsonDecode($result);
  42. // 记录日志
  43. log_record(['name' => '查询物流轨迹', 'provider' => 'aliyun', 'param' => $querys, 'result' => $data]);
  44. // 格式化返回的数据
  45. return $this->formatTraces($data['Traces']);
  46. }
  47. /**
  48. * 格式化返回的数据
  49. * @param array $source
  50. * @return array
  51. */
  52. private function formatTraces(array $source): array
  53. {
  54. return array_map(function ($item) {
  55. return ['time' => $item['AcceptTime'], 'context' => $item['AcceptStation']];
  56. }, array_reverse($source));
  57. }
  58. /**
  59. * curl请求指定url
  60. * @param string $url
  61. * @param array $headers
  62. * @param array $querys
  63. * @return bool|mixed|string
  64. * @throws BaseException
  65. */
  66. protected function curlGet(string $url, array $headers, array $querys)
  67. {
  68. $ch = curl_init();
  69. curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($querys));
  70. curl_setopt($ch, CURLOPT_HEADER, true);
  71. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  72. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  74. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  75. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  76. $result = curl_exec($ch);
  77. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  78. [$header, $body] = explode("\r\n\r\n", $result, 2);
  79. curl_close($ch);
  80. if ($httpCode == 200) {
  81. return $body;
  82. }
  83. $message = '参数名错误或其他错误';
  84. if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
  85. $message = '参数错误';
  86. } elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
  87. $message = 'AppCode错误';
  88. } elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
  89. $message = '请求的 Method、Path 或者环境错误';
  90. } elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
  91. $message = '服务未被授权(或URL和Path不正确)';
  92. } elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
  93. $message = '套餐包次数用完';
  94. } elseif ($httpCode == 500) {
  95. $message = 'API网关错误';
  96. } elseif ($httpCode == 0) {
  97. $message = 'URL错误';
  98. }
  99. throwError('阿里云物流查询API失败:' . $message);
  100. }
  101. }