Kuaidi100.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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;
  13. use think\facade\Cache;
  14. use cores\traits\ErrorTrait;
  15. use app\common\library\helper;
  16. /**
  17. * 快递100API模块
  18. * Class Kuaidi100
  19. * @package app\common\library\express
  20. */
  21. class Kuaidi100
  22. {
  23. use ErrorTrait;
  24. // 物流跟踪查询API地址
  25. const QUERY_URL = 'http://poll.kuaidi100.com/poll/query.do';
  26. // 微信支付配置
  27. /* @var array $config */
  28. private $config;
  29. /**
  30. * 构造方法
  31. * WxPay constructor.
  32. * @param $config
  33. */
  34. public function __construct($config)
  35. {
  36. $this->config = $config;
  37. }
  38. /**
  39. * 执行查询
  40. * @param string $code
  41. * @param string $expressNo
  42. * @return string|bool
  43. */
  44. public function query(string $code, string $expressNo)
  45. {
  46. // 缓存索引
  47. $cacheIndex = "express_{$code}_$expressNo";
  48. if ($cacheData = Cache::get($cacheIndex)) {
  49. return $cacheData;
  50. }
  51. // 参数设置
  52. $postData = [
  53. 'customer' => $this->config['customer'],
  54. 'param' => helper::jsonEncode([
  55. 'resultv2' => '1',
  56. 'com' => $code,
  57. 'num' => $expressNo
  58. ])
  59. ];
  60. $postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer']));
  61. // 请求快递100 api
  62. $result = $this->curlGet(self::QUERY_URL, $postData);
  63. $express = helper::jsonDecode($result);
  64. // 记录错误信息
  65. if (isset($express['returnCode']) || !isset($express['data'])) {
  66. $this->error = $express['message'] ?? '查询失败';
  67. return false;
  68. }
  69. // 记录缓存, 时效30分钟
  70. Cache::set($cacheIndex, $express['data'], 3000);
  71. return $express['data'];
  72. }
  73. /**
  74. * curl请求指定url (post)
  75. * @param $url
  76. * @param array $data
  77. * @return bool|string
  78. */
  79. private function curlGet($url, array $data = [])
  80. {
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_HEADER, 0);
  83. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  84. curl_setopt($ch, CURLOPT_URL, $url);
  85. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  86. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  87. $result = curl_exec($ch);
  88. curl_close($ch);
  89. return $result;
  90. }
  91. }