| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- //namespace app\common\service;
- namespace addons\pickup\library;
- use think\Cache;
- use think\Config;
- use GuzzleHttp\Client;
- /**
- * 地区数据模型
- */
- class Kuaidi100 {
- static protected $kuaidi100_key = "";
- static protected $kuaidi100_customer = "";
- static protected $kuaidi100_cache = 60 * 5;
- static protected $kuaidi100_url = "http://poll.kuaidi100.com/poll/query.do";
- static public function config() {
- $Config = get_addon_config('pickup');
- self::$kuaidi100_key = $Config['kuaidi100_key'];
- self::$kuaidi100_customer = $Config['kuaidi100_customer'];
- self::$kuaidi100_cache = $Config['kuaidi100_cache'];
- }
- static public function getKdInfo($param) {
- self::config();
- $param = array_merge([
- 'com' => '', //快递公司编码
- 'num' => '', //快递单号
- 'phone' => '', //手机号
- 'from' => '', //出发地城市
- 'to' => '', //目的地城市
- 'resultv2' => '1' //开启行政区域解析
- ], $param);
- $post_data = array();
- $post_data["customer"] = self::$kuaidi100_customer;
- $post_data["param"] = json_encode($param);
- $sign = md5($post_data["param"] . self::$kuaidi100_key . $post_data["customer"]);
- $post_data["sign"] = strtoupper($sign);
- $cacheName = "kd_info_" . $sign;
- if (Cache::has($cacheName)) {
- $data = Cache::get($cacheName);
- $res = json_decode($data, true);
- if ($res) {
- return $res;
- }
- }
- $client = new Client(['verify' => false]);
- $response = $client->request('POST', self::$kuaidi100_url, ['form_params' => $post_data])->getBody()->getContents();
- $data = \GuzzleHttp\json_decode($response, true);
- Cache::set($cacheName, json_encode($data), self::$kuaidi100_cache);
- return $data;
- }
- }
|