Kd100.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * 接口文档: 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(function ($item) {
  68. return ['time' => $item['ftime'], 'context' => $item['context']];
  69. }, $source);
  70. }
  71. /**
  72. * curl请求指定url
  73. * @param string $url
  74. * @param array $param
  75. * @return bool|string
  76. */
  77. protected function curlPost(string $url, array $param = [])
  78. {
  79. $ch = curl_init();
  80. curl_setopt($ch, CURLOPT_URL, $url);
  81. curl_setopt($ch, CURLOPT_POST, true);
  82. curl_setopt($ch, CURLOPT_HEADER, false);
  83. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  84. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
  85. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  86. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  87. $result = curl_exec($ch);
  88. curl_close($ch);
  89. return $result;
  90. }
  91. }