| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace app\librarys\KuaiDi100;
- use app\utils\CurlHelper;
- /**
- * KuaiDi100.php
- * todo 文件描述
- * Created on 2024/11/28 上午10:11
- * @author: hankaige
- */
- class KuaiDi100
- {
- private $key;
- private $secret;
- public function __construct($config)
- {
- $this->key = $config['key'];
- $this->secret = $config['secret'];
- }
- // 制作签名
- private function makeSign($params, $t): string
- {
- return strtoupper(md5($params.$t.$this->key.$this->secret));
- }
- // 获取智能地址解析返回结构
- public function addressResolution($addressStr){
- $url = "https://api.kuaidi100.com/address/resolution";
- list($msec, $sec) = explode(' ', microtime());
- $t = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- $param = json_encode(['content' => $addressStr], JSON_UNESCAPED_UNICODE);
- $postData = [
- 'key' => $this->key,
- 't' => $t,
- 'sign' => $this->makeSign($param,$t),
- 'param' => $param,
- ];
- $res = http_post($url,[
- 'header' => [
- 'Content-Type' => "application/x-www-form-urlencoded"
- ],
- 'form_params' => $postData
- ]);
- if ($res->getStatusCode() != 200) {
- return [
- 'code' => 1,
- 'msg' => '请求出错',
- ];
- }
- return json_decode((string)$res->getBody(), true);
- }
- }
|