TP5 quickly uses the method of Tencent SMS interface encapsulation

Just talk about the method directly .
1. Please download the attachments by yourself ( SmsMultiSender.php , SmsSenderUtil.php , SmsSingleSender ) There are three files, please note that my namespace is namespace sms; download and decompress them and put them in the sms folder (note that the three files are placed in the sms root directory
2. Put the sms folder in the extend class library directory under the TP5 directory .
3. How to use: first introduce
use sms\SmsMultiSender;
use sms\SmsSenderUtil;
use sms\SmsSingleSender;
4. I added an official demo to the attachment, the title has been indicated, remember not to run it directly, you can copy it and make a function to call, by the way, I will also paste the function to generate the verification code. Inside, see the notes for the specific usage.


function sendsms($tel){
try {
// Please develop according to the actual appid and appkey, the following is only used as a demonstration sdk
$appid = 000000;//Please apply by yourself and fill
in $appkey = "d0000000"; //Please apply by yourself Fill
in $templId = 0000000;//Please apply and fill
in $singleSender = new \sms\SmsSingleSender($appid, $appkey);//No need to explain it
//Specify the template single delivery
//Assume the template content is: test SMS, {1}, {2}, {3},
$mobilelz=$this->generate_code();//Generate verification code
//Write mobile phone number and verification code into database

$where['mobile']=$ tel;
$user=Db::name('mobile')->where($where)->find();//Check if there is a registration before writing to the database
if(!$user){//If there is no registration Register first
$member['mobile']=$tel;
$member['mobilelz']=$mobilelz;
$member['credate']=date('Ymd H:i:s');//create time
$member ['enddate']=date('Ymd H:i:s',strtotime('+180 second'));//The verification code will expire after 3 minutes.
$mobid=Db::name('mobile')->insert($member,false,true);//Write to the database
if($mobid){//The verification code will not be sent until the verification code is successfully written to the database
$params = array($mobilelz, "3");//The SMS template I applied for has only two parameters $mobilelz this is the generated random verification code "3", it will be invalid after 3 minutes after receiving the SMS message
$result = $singleSender->sendWithParam ("86", $member['mobile'], $templId, $params, "", "", "");//Start texting
$rsp = json_decode($result);
if($result=null) { //If no data is returned, it means that the message was not sent successfully. There seems to be a problem here. You have to check the official explanation to improve it. I hope you can propose a good way to deal with it.
Db::name('mobile')->where(' mobile',$tel)->delete(); //If the sending is not successful, the written data should be cleared
return false;
}else{
return $result; //Return the information about the SMS, you can use the dump display to see the return Get those information
}
}else{
return false;//If there is a problem with the data writing, return false
}

}elseif((date('Ymd H:i:s')>$user['enddate'])){//The current time is greater than the expiration time
Db::name('mobile')->where('mobile',$tel)->delete();//If it fails, delete it first to avoid redundancy
return ['error'=>'mobile verification code invalid' ];
}

} catch (\Exception $e) {
echo var_dump($e);
}
}

修正了
function sendsms($tel){
try {
// 请根据实际 appid 和 appkey 进行开发,以下只作为演示 sdk 使用
$appid = 00000;
$appkey = "0000000";
$templId = 00000;
//$phoneNumber1 = $tel;
$singleSender = new \sms\SmsSingleSender($appid, $appkey);
// 指定模板单发
// 假设模板内容为:测试短信,{1},{2},{3},
$mobilelz=$this->generate_code();//生成验证码
//把手机号码和验证码写入数据库

$where['mobile']=$tel;
$user=Db::name('mobile')->where($where)->find();
if(!$user){
$member['mobile']=$tel;
$member['mobilelz']=$mobilelz;
$member['credate']=date('Y-m-d H:i:s');//创建时间
$member['enddate']=date('Y-m-d H:i:s',strtotime('+180 second'));//验证码失效时间
$mobid=Db::name('mobile')->insert($member,false,true);//没有登记就写入
if($mobid){//写入库成功才到验证码发送出去
$params = array($mobilelz, "5");
$result = $singleSender->sendWithParam("86", $member['mobile'], $templId, $params, "", "", "");
$rsp = json_decode($result);
if($rsp['result']!=0){//放送失败的话
Db::name('mobile')->where('mobile',$tel)->delete();
return false;
}else{
return true; //返回短信信息
}
}else{
return false;
}

}elseif((date('Y-m-d H:i:s')>$user['enddate'])){//当前时候大于失效时间
Db::name('mobile')->where('mobile',$tel)->delete();
return false;
}

} catch (\Exception $e) {
echo var_dump($e);
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811199&siteId=291194637