php-腾讯云-短信-模板

腾讯云-短信-模板

  • 发送短信
setTemplId($templId);
$Sms->setValidTime($validTime);
$Sms->setPhone($phone);
return $Sms->sendSms();
/*end 短信 end*/

require_once "./SmsSingleSender.php";
/**
 * 短信接口
 * Created by Sublime.
 * User: dongfh
 * Date: 2020/07/24
 */
class Sms
{
/**
 * error code 业务错误说明.
 * 
    *
  • 1000: 成功
  • *
  • 1004: 失败
  • *
  • 1005: 业务失败
  • *
*/ private static $success = 1000; private static $fail = 1004; private static $business = 1005; private static $appid = "xxxx"; private static $appkey = "xxxx"; private static $nationCode = "86"; private static $smsSign = "中文签名"; private $templId = "xxxx"; public function setTemplId($templId){ $this->templId = $templId; } private $validTime = "5"; public function setValidTime($validTime){ $this->validTime = $validTime; } private $code; private $phone; public function setPhone($phone){ $this->phone = $phone; } private $SmsSingleSender; public function __construct(){ $this->SmsSingleSender = new SmsSingleSender(self::$appid,self::$appkey); } /** * [sendSms 发送验证码] * @return [type] [description] */ public function sendSms(){ //验证码 $this->getCode(); //验证码、有效分钟 $params = [$this->code,$this->validTime]; // 签名参数不能为空串 $result = $this->SmsSingleSender->sendWithParam(self::$nationCode,$this->phone,$this->templId,$params, self::$smsSign, "", ""); $rsp = json_decode($result,true); if($rsp["result"] == 0 && $rsp["errmsg"] == 'OK'){ return $this->JsonSuccess(["phone" => $this->phone],"发送验证码成功",self::$success); }else{ return $this->JsonError("发送验证码失败",self::$fail,$rsp); } } /** * [getCode 获取验证码] * @return [type] [description] */ private function getCode(){ $code = rand(100000, 999999); $is_set = Db::name("code")->where("code",$code)->find(); if(!empty($is_set)){ return $this->getCode(); } $codeData = [ "phone" => $this->phone, "code" => $code, "time" => time() ]; $result = Db::name("code")->insert($data); if(!$result){ return $this->JsonError("存储验证码失败",self::$fail,$codeData); } $this->code = $code; } /** * [JsonSuccess 返回数据] * @param [type] $data [数据] * @param string $msg [信息] * @param integer $code [状态码] */ private function JsonSuccess($data=[],$msg='成功',$code=1000){ $data = [ "code" => $code, "msg" => $msg, "data" => $data ]; return json_encode($data); } /** * [JsonError 返回数据] * @param integer $code [状态码] * @param string $msg [信息] * @param [type] $data [数据] */ private function JsonError($msg='失败',$code=1004,$data=[]){ return $this->JsonSuccess($data,$msg,$code); } }
  • 腾讯云短信类
url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
        $this->appid =  $appid;
        $this->appkey = $appkey;
        $this->util = new SmsSenderUtil();
    }

    /**
     * 普通单发
     *
     * 普通单发需明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名。
     *
     * @param int    $type        短信类型,0 为普通短信,1 营销短信
     * @param string $nationCode  国家码,如 86 为中国
     * @param string $phoneNumber 不带国家码的手机号
     * @param string $msg         信息内容,必须与申请的模板格式一致,否则将返回错误
     * @param string $extend      扩展码,可填空串
     * @param string $ext         服务端原样返回的参数,可填空串
     * @return string 应答json字符串,详细内容参见腾讯云协议文档
     */
    public function send($type, $nationCode, $phoneNumber, $msg, $extend = "", $ext = "")
    {
        $random = $this->util->getRandom();
        $curTime = time();
        $wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;

        // 按照协议组织 post 包体
        $data = new \stdClass();
        $tel = new \stdClass();
        $tel->nationcode = "".$nationCode;
        $tel->mobile = "".$phoneNumber;

        $data->tel = $tel;
        $data->type = (int)$type;
        $data->msg = $msg;
        $data->sig = hash("sha256",
            "appkey=".$this->appkey."&random=".$random."&time="
            .$curTime."&mobile=".$phoneNumber, FALSE);
        $data->time = $curTime;
        $data->extend = $extend;
        $data->ext = $ext;

        return $this->util->sendCurlPost($wholeUrl, $data);
    }

    /**
     * 指定模板单发
     *
     * @param string $nationCode  国家码,如 86 为中国
     * @param string $phoneNumber 不带国家码的手机号
     * @param int    $templId     模板 id
     * @param array  $params      模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数
     * @param string $sign        签名,如果填空串,系统会使用默认签名
     * @param string $extend      扩展码,可填空串
     * @param string $ext         服务端原样返回的参数,可填空串
     * @return string 应答json字符串,详细内容参见腾讯云协议文档
     */
    public function sendWithParam($nationCode, $phoneNumber, $templId = 0, $params,
        $sign = "", $extend = "", $ext = "")
    {
        $random = $this->util->getRandom();
        $curTime = time();
        $wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;

        // 按照协议组织 post 包体
        $data = new \stdClass();
        $tel = new \stdClass();
        $tel->nationcode = "".$nationCode;
        $tel->mobile = "".$phoneNumber;

        $data->tel = $tel;
        $data->sig = $this->util->calculateSigForTempl($this->appkey, $random,
            $curTime, $phoneNumber);
        $data->tpl_id = $templId;
        $data->params = $params;
        $data->sign = $sign;
        $data->time = $curTime;
        $data->extend = $extend;
        $data->ext = $ext;

        return $this->util->sendCurlPost($wholeUrl, $data);
    }
}
  • 腾讯云短信类
nationcode = $nationCode;
            $telElement->mobile = $phoneNumbers[$i];
            array_push($tel, $telElement);
        } while (++$i < count($phoneNumbers));

        return $tel;
    }

    /**
     * 生成签名
     *
     * @param string $appkey        sdkappid对应的appkey
     * @param string $random        随机正整数
     * @param string $curTime       当前时间
     * @param array  $phoneNumber   手机号码
     * @return string  签名结果
     */
    public function calculateSigForTempl($appkey, $random, $curTime, $phoneNumber)
    {
        $phoneNumbers = array($phoneNumber);

        return $this->calculateSigForTemplAndPhoneNumbers($appkey, $random,
            $curTime, $phoneNumbers);
    }

    /**
     * 生成签名
     *
     * @param string $appkey        sdkappid对应的appkey
     * @param string $random        随机正整数
     * @param string $curTime       当前时间
     * @return string 签名结果
     */
    public function calculateSigForPuller($appkey, $random, $curTime)
    {
        return hash("sha256", "appkey=".$appkey."&random=".$random
            ."&time=".$curTime);
    }

    /**
     * 生成上传文件授权
     *
     * @param string $appkey        sdkappid对应的appkey
     * @param string $random        随机正整数
     * @param string $curTime       当前时间
     * @param array  $fileSha1Sum   文件sha1sum
     * @return string  授权结果
     */
    public function calculateAuth($appkey, $random, $curTime, $fileSha1Sum)
    {
        return hash("sha256", "appkey=".$appkey."&random=".$random
            ."&time=".$curTime."&content-sha1=".$fileSha1Sum);
    }

    /**
     * 生成sha1sum
     *
     * @param string $content  内容
     * @return string  内容sha1散列值
     */
    public function sha1sum($content)
    {
        return hash("sha1", $content);
    }

    /**
     * 发送请求
     *
     * @param string $url      请求地址
     * @param array  $dataObj  请求内容
     * @return string 应答json字符串
     */
    public function sendCurlPost($url, $dataObj)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

        $ret = curl_exec($curl);
        if (false == $ret) {
            // curl_exec failed
            $result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
        } else {
            $rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp
                        . " " . curl_error($curl) ."\"}";
            } else {
                $result = $ret;
            }
        }

        curl_close($curl);

        return $result;
    }

    /**
     * 发送请求
     *
     * @param string $req  请求对象
     * @return string 应答json字符串
     */
    public function fetch($req)
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $req->url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $req->headers);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $req->body);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

        $result = curl_exec($curl);

        if (false == $result) {
            // curl_exec failed
            $result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
        } else {
            $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if (200 != $code) {
                $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $code
                    . " " . curl_error($curl) ."\"}";
            }
        }
        curl_close($curl);

        return $result;
    }
}

你可能感兴趣的:(php-腾讯云-短信-模板)