一、发送注册验证码代码实现
参考腾讯云文档 https://cloud.tencent.com/document/product/382/56058
1.通过composer安装
composer require tencentcloud/tencentcloud-sdk-php
2.TP6框架相关代码 2.1添加相关接口方法
use app\common\controller\SmsController;
public function sendCode(){
$phone = request()->post('phone');
if (!isMobile($phone)){
return SendError("手机号码不正常");
}
$sms = new SmsController($this->app);
$sms->sendcode($phone);
if ($sms->getError()){
return SendError($sms->getError());
}
return SendSuccess("发送成功");
}
2.2 在app/common文件夹下新建smscontroler类
namespace app\common\controller;
use TencentCloud\Sms\V20190711\SmsClient;
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
class SmsController extends BaseController{
protected $SecretId;
protected $SecretKey;
protected $AppID;
protected $smsSign;
protected $templateId;
public $error ='';
private $expire_time = 600;
private $wait_time = 120;
public function initialize()
{
parent::initialize();
$this->SecretId = env('SMS.sid', '');
$this->SecretKey = env('SMS.skey', '');
$this->AppID = env('SMS.appid', '');
$this->smsSign = env('SMS.sign', '');
$this->templateId = "493349";
}
public function sendcode($phone='',$t_type=1){
$phone ="+86".$phone;
switch ($t_type){
case 2:
$this->templateId = "493349";
break;
default:
$this->templateId = "493349";
}
$TemplateParamSet = $this->setVcode($phone);
if ($this->error){
return false;
}
try {
$cred = new Credential($this->SecretId, $this->SecretKey);
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("sms.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new SmsClient($cred, "", $clientProfile);
$req = new SendSmsRequest();
$params = '{"PhoneNumberSet":["'.$phone.'"],"TemplateID":"'.$this->templateId.'",
"Sign":"'.$this->smsSign.'","TemplateParamSet":["'.$TemplateParamSet.'"],"SmsSdkAppid":"'.$this->AppID.'"}';
$req->fromJsonString($params);
$resp = $client->SendSms($req);
if ($resp->SendStatusSet[0]->Code !='Ok'){
$this->error ='发送失败';
return false;
}
return true;
}
catch(TencentCloudSDKException $e) {
echo $e;
}
}
public function setVcode($account){
$key = get_vcode_key($account);
$vcodeData = cache($key);
if (is_array($vcodeData) && !empty($vcodeData['when_send'])){
if( time() - $vcodeData['when_send'] < $this->wait_time ){
$this->error = '操作过于频繁,请稍后重试';
return false;
}
}else{
$vcodeData = array();
$vcode = rand(100000,999999);
$vcodeData['mobile'] = $account;
$vcodeData['vcode'] = $vcode;
$vcodeData['when_send'] = time();
cache($key,$vcodeData,$this->expire_time);
return $vcode;
}
}
public function checkVcode($account, $vcode){
$key = get_vcode_key($account);
$vcodeData = cache($key);
if ($vcodeData) {
if($vcodeData['mobile']==$account && $vcodeData['vcode']==$vcode){
if(time()-$vcodeData['when_send'] > $this->expire_time){
$this->error = '验证码已过期';
cache($key,NULL);
return false;
}else{
return true;
}
}else{
$this->error = '验证码不正确';
return false;
}
}else{
$this->error = '验证失败';
return false;
}
}
public function getError(){
return $this->error;
}
}
二、错误解决
安装SDK之后,如果您的 PHP 环境证书有问题,可能会遇到报错,类似于 cURL error 60: See http://curl.haxx.se/libcurl/c/libcurl-errors.html, 请尝试按以下步骤解决:
1.到 https://curl.haxx.se/ca/cacert.pem 下载证书文件cacert.pem。
将 cacert.pem文件复制到php安装目录中,如下,我的路经
E:\phpstudy_pro\Extensions\php\php7.4.3nts\extras\ssl\cacert.pem
2.编辑php.ini文件。
删除curl.cainfo配置项前的分号注释符(;)
配置curl.cainfo绝对路经,如下
curl.cainfo =E:\phpstudy_pro\Extensions\php\php7.4.3nts\extras\ssl\cacert.pem
3.重启依赖 PHP 的服务。
|