You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.9 KiB
PHTML

9 months ago
<?php
9 months ago
var_dump(getIpInfo('120.32.100.138'));
9 months ago
9 months ago
function getIpInfo($ip) {
9 months ago
// 云市场分配的密钥Id
$secretId = 'AKID5tH3om0m0dPG4KMBLFLp03E4zrOF0a1qTCXh';
// 云市场分配的密钥Key
$secretKey = 'J06iJMIphW7P6j2KvOr9lMk3EuD9UkJ1ltVo2R3p';
$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 = 'POST';
// 请求头
$headers = array(
'X-Source' => $source,
'X-Date' => $datetime,
'Authorization' => $auth,
);
// 查询参数
$queryParams = array (
);
// body参数POST方法下
$bodyParams = array (
9 months ago
'ip' => $ip,
9 months ago
);
// url参数拼接
$url = 'https://service-hnhpr5tw-1305308687.gz.apigw.tencentcs.com/release/ip/query';
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)) {
9 months ago
return [
"success" => false,
"code" => 800,
"data" => null,
"msg" => "请求失败"
];
9 months ago
} else {
9 months ago
return json_decode($data, true);
9 months ago
}
curl_close($ch);
}