首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >sms提示找不到version?

sms提示找不到version?

提问于 2020-12-11 14:35:54
回答 2关注 0查看 305
代码语言:javascript
复制
<?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);



回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档