该代码实现了一个名为Tensms的类,用于通过腾讯云发送短信。它包含配置、请求方法、签名生成和CURL请求等功能。类构造函数初始化配置,提供了设置请求协议、方法和选项的功能,并实现了发送短信的请求逻辑。通过生成签名和设置请求头,确保请求的安全性和有效性。
<?php
declare (strict_types = 1);
namespace system\sms;
class Tensms {
/**
* @var array
*/
public $config = [];
/**
* @var array
*/
public $options = [];
/**
* @var string
*/
public $version = '2019-07-11';
/**
* @var string
*/
public $action = 'SendSms';
/**
* @var string
*/
public $protocol = 'https://';
/**
* @var string
*/
public $host = 'sms.tencentcloudapi.com';
/**
* @var string
*/
public $algorithm = "TC3-HMAC-SHA256";
/**
* @var string
*/
public $method = 'POST';
/**
* @var string
*/
public $service = 'sms';
/**
* @var string
*/
public $timestamp = 0;
/**
* @var object 对象实例
*/
protected static $instance = null;
/**
* 类构造函数
* class constructor.
*/
public function __construct(array $config)
{
$this->config = $config;
$this->timestamp = time();
}
/**
* 初始化
* @access public
* @param array $options 参数
* @return static
*/
public static function instance(array $options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
/**
* @param string $action
*
* @return $this
*/
public function action($action)
{
$this->action = $action;
return $this;
}
/**
* @param array $options
*
* @return $this
*/
public function options(array $options)
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
return $this;
}
/**
* @param string $method
*
* @return $this
*/
public function method($method)
{
$this->method = $method;
return $this;
}
/**
* 设置请求协议
* @param string $protocol 请求协议(https:// http://)
*/
public function setProtocol($protocol)
{
$this->protocol = $protocol;
}
/**
* 短信发送函数
* @return array|false
*/
public function request()
{
$this->options = is_array($this->options) ? json_encode($this->options, JSON_UNESCAPED_UNICODE) : $this->options;
$hashedRequestPayload = $this->RequestPayload($this->options);
// 拼接字符串
$canonicalRequest = $this->method."\n"."/\n"."\n"."content-type:application/json\n"
."host:".$this->host."\n\n"."content-type;host\n".$hashedRequestPayload;
// 获取时间戳
$date = gmdate("Y-m-d", $this->timestamp);
$credentialScope = $date."/".$this->service."/tc3_request";
$stringToSign = $this->stringToSign($canonicalRequest, $credentialScope);
// 获取真实签名
$signature = $this->signature($stringToSign, $date);
$authorization = $this->authorization($credentialScope, $signature);
// 使用 CURL 发送请求
$result = $this->curlPost($this->protocol.$this->host, $this->options, $this->header($authorization));
if ($result === false) {
return false;
}
return json_decode($result, true);
}
/**
* 返回数据哈希值
* @access protected
* @param mixed $payload
*/
protected function RequestPayload($payload)
{
return hash("SHA256", $payload);
}
/**
* 返回待签名字符串
* @access protected
* @param mixed $sign
* @param mixed $param
*/
protected function stringToSign($canonicalRequest, $credentialScope)
{
$hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
return $this->algorithm."\n".$this->timestamp."\n".$credentialScope."\n".$hashedCanonicalRequest;
}
/**
* 返回真实签名
* @access protected
* @param mixed $stringToSign
* @param mixed $date
*/
protected function signature($stringToSign, $date)
{
$secretDate = hash_hmac("SHA256", $date, "TC3".$this->config['secret_key'], true);
$secretService = hash_hmac("SHA256", $this->service, $secretDate, true);
$secretSigning = hash_hmac("SHA256", "tc3_request", $secretService, true);
return hash_hmac("SHA256", $stringToSign, $secretSigning);
}
/**
* 返回数据签名串
* @access protected
* @param string $credentialScope
* @param string $signature
*/
protected function authorization($credentialScope, $signature)
{
return $this->algorithm." Credential=".$this->config['secret_id']."/".$credentialScope .", SignedHeaders=content-type;host, Signature=".$signature;
}
/**
* 设置头部信息
* @access protected
* @param string $authorization
*/
protected function header($authorization)
{
return [
'Content-Type: application/json',
'X-TC-Action: SendSms',
'X-TC-Timestamp: '.$this->timestamp,
'X-TC-Version: 2019-07-11',
'X-TC-Language: zh-CN',
'Authorization: '.$authorization,
];
}
/**
* 使用 CURL 发送 POST 请求
* @param string $url
* @param string $postData
* @param array $headers
* @return string|false
*/
protected function curlPost($url, $postData, $headers)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
#案例
$config = [
'secret_id' => 'your_secret_id',
'secret_key' => 'your_secret_key',
];
$tensms = new \system\sms\Tensms($config);
$tensms->options([
'PhoneNumberSet' => ['+8613711112222'], // 设置发送短信的号码
'TemplateID' => '123456', // 短信模板ID
'Sign' => 'YourSign', // 短信签名
'TemplateParamSet' => ['验证码', '1234'], // 模板参数
]);
$response = $tensms->request();
var_dump($response);