Kuaidi100.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 app\common\library\helper;
  15. /**
  16. * 快递100API模块
  17. * Class Kuaidi100
  18. * @package app\common\library\express
  19. */
  20. class Kuaidi100
  21. {
  22. /* @var array $config 微信支付配置 */
  23. private $config;
  24. /* @var string $error 错误信息 */
  25. private $error;
  26. /**
  27. * 构造方法
  28. * WxPay constructor.
  29. * @param $config
  30. */
  31. public function __construct($config)
  32. {
  33. $this->config = $config;
  34. }
  35. /**
  36. * 执行查询
  37. * @param $code
  38. * @param $expressNo
  39. * @return bool
  40. */
  41. public function query($code, $expressNo)
  42. {
  43. // 缓存索引
  44. $cacheIndex = "express_{$code}_$expressNo";
  45. if ($data = Cache::instance()->get($cacheIndex)) {
  46. return $data;
  47. }
  48. // 参数设置
  49. $postData = [
  50. 'customer' => $this->config['customer'],
  51. 'param' => helper::jsonEncode([
  52. 'resultv2' => '1',
  53. 'com' => $code,
  54. 'num' => $expressNo
  55. ])
  56. ];
  57. $postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer']));
  58. // 请求快递100 api
  59. $url = 'http://poll.kuaidi100.com/poll/query.do';
  60. $result = curl_post($url, http_build_query($postData));
  61. $express = helper::jsonDecode($result);
  62. // 记录错误信息
  63. if (isset($express['returnCode']) || !isset($express['data'])) {
  64. $this->error = isset($express['message']) ? $express['message'] : '查询失败';
  65. return false;
  66. }
  67. // 记录缓存, 时效5分钟
  68. Cache::instance()->set($cacheIndex, $express['data'], 300);
  69. return $express['data'];
  70. }
  71. /**
  72. * 返回错误信息
  73. * @return string
  74. */
  75. public function getError()
  76. {
  77. return $this->error;
  78. }
  79. }