Kd100.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * 快递100物流查询驱动
  18. * Class Kd100
  19. * @package app\common\library\express\provider\driver
  20. */
  21. class Kd100 extends Driver
  22. {
  23. // API地址
  24. const API_URL = 'https://poll.kuaidi100.com/poll/query.do';
  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. $param = [
  36. 'customer' => $this->options['customer'],
  37. 'param' => helper::jsonEncode([
  38. 'resultv2' => '1',
  39. 'com' => $code,
  40. 'num' => $expressNo
  41. ])
  42. ];
  43. $param['sign'] = strtoupper(md5($param['param'] . $this->options['key'] . $param['customer']));
  44. // 请求API
  45. $result = $this->curlPost(self::API_URL, $param);
  46. $data = helper::jsonDecode($result);
  47. // 记录日志
  48. log_record(['name' => '查询物流轨迹', 'provider' => 'kd100', 'param' => $param, 'result' => $data]);
  49. // 记录错误信息
  50. if (isset($data['returnCode']) || !isset($data['data'])) {
  51. throwError('快递100物流查询API失败:' . $data['message']);
  52. }
  53. // 格式化返回的数据
  54. return $this->formatTraces($data['data']);
  55. }
  56. /**
  57. * 格式化返回的数据
  58. * @param array $source
  59. * @return array
  60. */
  61. private function formatTraces(array $source): array
  62. {
  63. return array_map(function ($item) {
  64. return ['time' => $item['ftime'], 'context' => $item['context']];
  65. }, $source);
  66. }
  67. /**
  68. * curl请求指定url
  69. * @param string $url
  70. * @param array $param
  71. * @return bool|string
  72. */
  73. protected function curlPost(string $url, array $param = [])
  74. {
  75. $ch = curl_init();
  76. curl_setopt($ch, CURLOPT_URL, $url);
  77. curl_setopt($ch, CURLOPT_POST, true);
  78. curl_setopt($ch, CURLOPT_HEADER, false);
  79. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  80. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
  81. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  82. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  83. $result = curl_exec($ch);
  84. curl_close($ch);
  85. return $result;
  86. }
  87. }