Kuaidi100.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. //namespace app\common\service;
  3. namespace addons\pickup\library;
  4. use think\Cache;
  5. use think\Config;
  6. use GuzzleHttp\Client;
  7. /**
  8. * 地区数据模型
  9. */
  10. class Kuaidi100 {
  11. static protected $kuaidi100_key = "";
  12. static protected $kuaidi100_customer = "";
  13. static protected $kuaidi100_cache = 60 * 5;
  14. static protected $kuaidi100_url = "http://poll.kuaidi100.com/poll/query.do";
  15. static public function config() {
  16. $Config = get_addon_config('pickup');
  17. self::$kuaidi100_key = $Config['kuaidi100_key'];
  18. self::$kuaidi100_customer = $Config['kuaidi100_customer'];
  19. self::$kuaidi100_cache = $Config['kuaidi100_cache'];
  20. }
  21. static public function getKdInfo($param) {
  22. self::config();
  23. $param = array_merge([
  24. 'com' => '', //快递公司编码
  25. 'num' => '', //快递单号
  26. 'phone' => '', //手机号
  27. 'from' => '', //出发地城市
  28. 'to' => '', //目的地城市
  29. 'resultv2' => '1' //开启行政区域解析
  30. ], $param);
  31. $post_data = array();
  32. $post_data["customer"] = self::$kuaidi100_customer;
  33. $post_data["param"] = json_encode($param);
  34. $sign = md5($post_data["param"] . self::$kuaidi100_key . $post_data["customer"]);
  35. $post_data["sign"] = strtoupper($sign);
  36. $cacheName = "kd_info_" . $sign;
  37. if (Cache::has($cacheName)) {
  38. $data = Cache::get($cacheName);
  39. $res = json_decode($data, true);
  40. if ($res) {
  41. return $res;
  42. }
  43. }
  44. $client = new Client(['verify' => false]);
  45. $response = $client->request('POST', self::$kuaidi100_url, ['form_params' => $post_data])->getBody()->getContents();
  46. $data = \GuzzleHttp\json_decode($response, true);
  47. Cache::set($cacheName, json_encode($data), self::$kuaidi100_cache);
  48. return $data;
  49. }
  50. }