<?php
namespace common\services;
class TencentCloudService extends BaseService
{
private $SMSParams = [
'common'=> [
'secretId' => 'xxx',
'secretKey' => 'xxx',
'host' => 'sms.tencentcloudapi.com',
'service' => 'sms',
],
'SendSms' => [ //发送短信
'Action' => 'SendSms',
'Version' => '2019-07-11',
],
'DescribeSmsSignList' => [ //查询签名列表
'Action' => 'DescribeSmsSignList',
'Version' => '2019-07-11',
],
];
/**
* @var array|mixed
*/
private $params;
/**
* 调用腾讯云api
* @param $api
* @param $data
* @return false
*/
public function postApi($api, $data)
{
$payload = json_encode($data);
foreach ($data as $key => $value) {
if (is_array($value)){
$data[$key] = json_encode($value);
}
}
if (!isset($this->SMSParams[$api])){
return false;
}
$this->params = array_merge($this->SMSParams['common'], $this->SMSParams[$api]);
$postHeader = $signHeader = [
"Content-Type" => "Content-Type:application/json",
"Host" => "Host:".$this->params['host'],
];
$timestamp = time();
$postHeader['Authorization'] = 'Authorization: '.$this->getAuthorization($payload, $timestamp, $signHeader);
$postHeader['X-TC-Action'] = 'X-TC-Action: '.$this->params['Action'];
$postHeader['X-TC-Timestamp'] = 'X-TC-Timestamp: '.$timestamp;
$postHeader['X-TC-Version'] = 'X-TC-Version: '.$this->params['Version'];
if ($this->params['region']?? false){
$postHeader['X-TC-Region'] = 'X-TC-Region: '.$this->params['region'];
}
// $this->toStr($this->params, $postHeader, $payload);
$curl = curl_init('https://'.$this->params['host']);
curl_setopt($curl,CURLOPT_HEADER,array_values($postHeader));
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($curl);
var_dump($output);
$content = curl_multi_getcontent($curl);
var_dump($content);
curl_close($curl);
exit();
}
/**
* @param $payload
* @param $timestamp
* @param $header
* @return string
*/
private function getAuthorization($payload, $timestamp, $header)
{
$algorithm = "TC3-HMAC-SHA256";
$lowerHeader = [];
foreach ($header as $key=>$item) {
$lowerHeader[strtolower($key)] = strtolower($item);
}
ksort($lowerHeader);
$canonicalRequest = [
'httpRequestMethod' => "POST",
'canonicalUri' => "/",
'canonicalQueryString' => "",
'CanonicalHeaders' => implode("\n",$lowerHeader)."\n",
"signedHeaders" => implode(';',array_keys($lowerHeader)),
'hashedRequestPayload' => hash('SHA256', $payload)
];
$canonicalRequest = implode("\n",$canonicalRequest);
$date = gmdate("Y-m-d", $timestamp);
$credentialScope = $date."/".$this->params['service']."/tc3_request";
$sign = [
'algorithm' => $algorithm,
'timestamp' => $timestamp,
'credentialScope' => $credentialScope,
'hashedCanonicalRequest' => hash("SHA256", $canonicalRequest)
];
$stringToSign = implode("\n", $sign);
$secretDate = hash_hmac("SHA256", $date, "TC3".$this->params['secretKey'], true);
$secretService = hash_hmac("SHA256", $this->params['service'], $secretDate, true);
$secretSigning = hash_hmac("SHA256", "tc3_request", $secretService, true);
$signature = hash_hmac("SHA256", $stringToSign, $secretSigning);
return $algorithm . ' ' .
"Credential=" . $this->params['secretId'] . "/" . $credentialScope .', ' .
"SignedHeaders=".implode(';',array_keys($lowerHeader)).", " .
"Signature=" . $signature;
}
private function toStr(array $params, array $postHeader, string $payload)
{
$str = 'curl ';
foreach ($postHeader as $item) {
$str .= ' -H \''.$item.'\'';
}
$str .= ' -d \''.$payload.'\'';
$str .= ' \'https://'.$params['host'].'\'';
echo '<br/>',$str,'<br/>';exit();
}
}
$data = [
'SignIdSet' => [
290498
],
'International' => 0
];
(new TencentCloudService())->postApi('DescribeSmsSignList',$data);
相似问题