Kuaidi100.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\library\express;
  10. use think\Cache;
  11. /**
  12. * 快递100API模块
  13. * Class Kuaidi100
  14. * @package app\common\library\express
  15. */
  16. class Kuaidi100
  17. {
  18. /* @var array $config 微信支付配置 */
  19. private $config;
  20. /* @var string $error 错误信息 */
  21. private $error;
  22. /**
  23. * 构造方法
  24. * WxPay constructor.
  25. * @param $config
  26. */
  27. public function __construct($config)
  28. {
  29. $this->config = $config;
  30. }
  31. /**
  32. * 执行查询
  33. * @param $express_code
  34. * @param $express_no
  35. * @return bool
  36. */
  37. public function query($express_code, $express_no)
  38. {
  39. // 缓存索引
  40. $cacheIndex = 'express_' . $express_code . '_' . $express_no;
  41. if ($data = Cache::get($cacheIndex)) {
  42. return $data;
  43. }
  44. // 参数设置
  45. $postData = [
  46. 'customer' => $this->config['customer'],
  47. 'param' => json_encode([
  48. 'resultv2' => '1',
  49. 'com' => $express_code,
  50. 'num' => $express_no
  51. ])
  52. ];
  53. $postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer']));
  54. // 请求快递100 api
  55. $url = 'http://poll.kuaidi100.com/poll/query.do';
  56. $result = curlPost($url, http_build_query($postData));
  57. $express = json_decode($result, true);
  58. // 记录错误信息
  59. if (isset($express['returnCode']) || !isset($express['data'])) {
  60. $this->error = isset($express['message']) ? $express['message'] : '查询失败';
  61. return false;
  62. }
  63. // 记录缓存, 时效5分钟
  64. Cache::set($cacheIndex, $express['data'], 300);
  65. return $express['data'];
  66. }
  67. /**
  68. * 返回错误信息
  69. * @return string
  70. */
  71. public function getError()
  72. {
  73. return $this->error;
  74. }
  75. }