现在各个大厂API写的是越来越大,一个curl能解决的问题要加载一堆组件,本站主要是涉及域名类操作,完全满足。写了一个函数可以直接封装到业务类中,下列代码做了最大化精简,放弃了GET场景,根据官方文档和POST有点不同,请自行魔改,不罗嗦直接上代码:
脱坑指示 TX端对数据类型做了严格认证,所以提交数据时要转换类型,比如 string int 大部分情况下,手机号属于 string。
function api_call($api, $version, $action, $data)
{
$secretId = "AK*****************************";
$secretKey = "OadUD**************************";
$host = $api . ".tencentcloudapi.com";
$timestamp = time();
$payload = json_encode($data);
$date = gmdate("Y-m-d", $timestamp);
$credentialScope = $date . "/" . $api . "/tc3_request";
$stringToSign = "TC3-HMAC-SHA256n"
. $timestamp . "n"
. $credentialScope . "n"
. hash("SHA256", "POSTn/nncontent-type:application/json; charset=utf-8n" . "host:" . $host . "nncontent-type;hostn" . hash("SHA256", $payload));
$signature = hash_hmac("SHA256", $stringToSign, hash_hmac("SHA256", "tc3_request", hash_hmac("SHA256", $api, hash_hmac("SHA256", $date, "TC3" . $secretKey, true), true), true));
$headers = [
'Authorization: ' . "TC3-HMAC-SHA256 Credential=" . $secretId . "/" . $credentialScope . ", SignedHeaders=content-type;host, Signature=" . $signature,
'Content-Type: application/json; charset=utf-8',
'Host: ' . $host,
'X-TC-Action: ' . $action,
'X-TC-Timestamp: ' . $timestamp,
'X-TC-Version: ' . $version,
'X-TC-Region: ap-guangzhou',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://' . $host);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$output = json_decode(curl_exec($ch), true);
curl_close($ch);
return $output;
}
//echo json_encode(api_call('dnspod', "2021-03-23", "DescribeRecordList", ["Domain" => "qq.cn"]));
//echo json_encode(api_call('domain', "2018-08-08", "DescribeDomainBaseInfo", ["Domain" => "qq.cn"]));