AddressParse.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Handlers;
  3. use http\Env\Request;
  4. class AddressParse {
  5. public function getAddress($discernDel)
  6. {
  7. // 云市场分配的密钥Id
  8. $secretId = 'AKID4X7w85Y57hOU5eH5xW4CpGXX08LaaCHdeZwa';
  9. // 云市场分配的密钥Key
  10. $secretKey = 'h2nsLx1djnIq3mN13vVa4d849K471c1dq511576';
  11. $source = 'market';
  12. // 签名
  13. $datetime = gmdate('D, d M Y H:i:s T');
  14. $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
  15. $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
  16. $auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign);
  17. // 请求方法
  18. $method = 'GET';
  19. // 请求头
  20. $headers = array(
  21. 'X-Source' => $source,
  22. 'X-Date' => $datetime,
  23. 'Authorization' => $auth,
  24. );
  25. // 查询参数
  26. $queryParams = array(
  27. 'UseFullMunicipality' => '',
  28. 'address' => $discernDel,
  29. );
  30. // body参数(POST方法下)
  31. $bodyParams = array();
  32. // url参数拼接
  33. $url = 'https://service-7daeqy5n-1301652365.bj.apigw.tencentcs.com/release/address_parse';
  34. if (count($queryParams) > 0) {
  35. $url .= '?' . http_build_query($queryParams);
  36. }
  37. $ch = curl_init();
  38. curl_setopt($ch, CURLOPT_URL, $url);
  39. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  40. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  41. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) {
  43. return $k . ': ' . $v;
  44. }, array_values($headers), array_keys($headers)));
  45. if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
  46. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));
  47. }
  48. $data = curl_exec($ch);
  49. if (curl_errno($ch)) {
  50. return "Error: " . curl_error($ch);
  51. } else {
  52. return $data;
  53. }
  54. curl_close($ch);
  55. }
  56. }