KuaiDi100.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\librarys\KuaiDi100;
  3. use app\utils\CurlHelper;
  4. /**
  5. * KuaiDi100.php
  6. * todo 文件描述
  7. * Created on 2024/11/28 上午10:11
  8. * @author: hankaige
  9. */
  10. class KuaiDi100
  11. {
  12. private $key;
  13. private $secret;
  14. public function __construct($config)
  15. {
  16. $this->key = $config['key'];
  17. $this->secret = $config['secret'];
  18. }
  19. // 制作签名
  20. private function makeSign($params, $t): string
  21. {
  22. return strtoupper(md5($params.$t.$this->key.$this->secret));
  23. }
  24. // 获取智能地址解析返回结构
  25. public function addressResolution($addressStr){
  26. $url = "https://api.kuaidi100.com/address/resolution";
  27. list($msec, $sec) = explode(' ', microtime());
  28. $t = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  29. $param = json_encode(['content' => $addressStr], JSON_UNESCAPED_UNICODE);
  30. $postData = [
  31. 'key' => $this->key,
  32. 't' => $t,
  33. 'sign' => $this->makeSign($param,$t),
  34. 'param' => $param,
  35. ];
  36. $res = http_post($url,[
  37. 'header' => [
  38. 'Content-Type' => "application/x-www-form-urlencoded"
  39. ],
  40. 'form_params' => $postData
  41. ]);
  42. if ($res->getStatusCode() != 200) {
  43. return [
  44. 'code' => 1,
  45. 'msg' => '请求出错',
  46. ];
  47. }
  48. return json_decode((string)$res->getBody(), true);
  49. }
  50. }