Kd100.php 3.5 KB

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