12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace App\Handlers;
- use http\Env\Request;
- class AddressParse {
- public function getAddress($discernDel)
- {
- // 云市场分配的密钥Id
- $secretId = 'AKID4X7w85Y57hOU5eH5xW4CpGXX08LaaCHdeZwa';
- // 云市场分配的密钥Key
- $secretKey = 'h2nsLx1djnIq3mN13vVa4d849K471c1dq511576';
- $source = 'market';
- // 签名
- $datetime = gmdate('D, d M Y H:i:s T');
- $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
- $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
- $auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign);
- // 请求方法
- $method = 'GET';
- // 请求头
- $headers = array(
- 'X-Source' => $source,
- 'X-Date' => $datetime,
- 'Authorization' => $auth,
- );
- // 查询参数
- $queryParams = array(
- 'UseFullMunicipality' => '',
- 'address' => $discernDel,
- );
- // body参数(POST方法下)
- $bodyParams = array();
- // url参数拼接
- $url = 'https://service-7daeqy5n-1301652365.bj.apigw.tencentcs.com/release/address_parse';
- if (count($queryParams) > 0) {
- $url .= '?' . http_build_query($queryParams);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 60);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) {
- return $k . ': ' . $v;
- }, array_values($headers), array_keys($headers)));
- if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));
- }
- $data = curl_exec($ch);
- if (curl_errno($ch)) {
- return "Error: " . curl_error($ch);
- } else {
- return $data;
- }
- curl_close($ch);
- }
- }
|