php发送腾讯云变量短信源码,php结合腾讯云短信实现功能!(发和单发都可以)...
namespace app\home\controller;
use core\basic\Controller;
use core\basic\Smsnotice;
use app\home\model\ParserModel;
//短信发送需要引⼊的⽂件
require './tencentsdk/vendor/autoload.php';
use TencentCloud\Sms\V20190711\SmsClient;
// 导⼊要请求接⼝对应的Request类
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 IndexController extends Controller{
protected $parser;
protected $model;
public function __construct()
{
$this->parser = new ParserController();
$this->model = new ParserModel();
}
// 表单提交短信发送验证
public function addForm(){
if ($_POST) { session_start();
if( trim($_POST['getNotice']) ){
$mobile = '+86'.trim($_POST['mobile']);
$randNums = mt_rand(100000,999999);
$appid = '******';
表格网站php源码$appkey = "******";
$templId = '****';
$SecretId = '******';
$SecretKey = '*****';
$sign = "您的审核通过签名";
try {
$cred = new Credential($SecretId, $SecretKey);
$httpProfile = new HttpProfile();
$httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
$httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
$httpProfile->setEndpoint("tcloudapi"); // 指定接⼊地域域名(默认就近接⼊)
// 实例化⼀个client选项,可选的,没有特殊需求可以跳过
$clientProfile = new ClientProfile();
$clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
$clientProfile->setHttpProfile($httpProfile);
// 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的
$client = new SmsClient($cred, "ap-shanghai", $clientProfile);
// 实例化⼀个 sms 请求对象,每个接⼝都会对应⼀个request对象。
$req = new SendSmsRequest();
$req->SmsSdkAppid = $appid;
/* 短信签名内容: 使⽤ UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
$req->Sign = $sign;
/* 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper] */
$req->ExtendCode = "0";
/* 下发⼿机号码,采⽤ e.164 标准,+[国家或地区码][⼿机号]
* ⽰例如:+86137********, 其中前⾯有⼀个+号 ,86为国家码,137********为⼿机号,最多不要超过200个⼿机号*/ $req->PhoneNumberSet = array($mobile);
/* 国际/港澳台短信 senderid: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */
$req->SenderId = "";
/* ⽤户的 session 内容: 可以携带⽤户侧 ID 等上下⽂信息,server 会原样返回 */
$req->SessionContext = "";
/* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
$req->TemplateID = $templId;
/* 模板参数: 若⽆模板参数,则设置为空*/
$req->TemplateParamSet = array("0"=>$randNums);
// 通过client对象调⽤DescribeInstances⽅法发起请求。注意请求⽅法名与请求对象是对应的
// 返回的resp是⼀个DescribeInstancesResponse类的实例,与请求对象对应
$resp = $client->SendSms($req);
$string = json_decode($resp->toJsonString(),true);
$statusCode = $string['SendStatusSet'][0]['Code'];
$status = $string['SendStatusSet'][0]['Message'];
if($statusCode=='Ok' && $status=='send success'){
$user = array(
'mobile' => trim($_POST['mobile']),
'code' => $randNums,
'time' => time()+300,
);
$_SESSION['user'] = $user;
echo json_encode(array('status'=>1,'info'=>'短信验证码发送成功!'));
exit;
}else{
echo json_encode(array('status'=>2,'info'=>'请勿频繁获取短信验证码,次数有限制!')); exit;
}
// 输出json格式的字符串回包
//print_r($resp->toJsonString());
// 也可以取出单个值。
// 你可以通过官⽹接⼝⽂档或跳转到response对象的定义处查看返回字段的定义
//print_r($resp->TotalCount);
}
catch(TencentCloudSDKException $e) {
echo $e;
}
}
//短信验证码时间爱你间隔问题
$stel = $_SESSION['user']['mobile'];
$ptel = trim($_POST['tel']);
$stime = $_SESSION['user']['time'];
$ptime = time();
$scode = strval($_SESSION['user']['code']);
$pcode = trim($_POST['code']);
if( $stime < $ptime ){
error('短信验证码已经失效,请返回重新点击获取短信验证码');
}
if( $scode !== $pcode ){
error('验证码错误,请返回重新输⼊短信验证码!');
}
if( $stel !==$ptel ){
error('输⼊的⼿机号码与接收到短信的⼿机号码不⼀致');
}
if ($this->config('form_status') === '0') {
error('系统已经关闭表单功能,请到后台开启再试!');
}
if (time() - session('lastsub') < 10) {
alert_back('您提交太频繁了,请稍后再试!');
}
if (! $fcode = get('fcode', 'var')) {
alert_back('传递的表单编码有误!');
}
if ($fcode == 1) {
alert_back('表单提交地址有误,留⾔提交请使⽤留⾔专⽤地址!'); }
// 验证码验证
$checkcode = strtolower(post('checkcode', 'var'));
if ($this->config('form_check_code')) {
if (! $checkcode) {
alert_back('验证码不能为空!');
}
if ($checkcode != session('checkcode')) {
alert_back('验证码错误!');
}
}
// 读取字段
if (! $form = $this->model->getFormField($fcode)) {
alert_back('接收表单不存在任何字段,请核对后重试!');
}
// 接收数据
$mail_body = '';
foreach ($form as $value) {
$field_data = post($value->name);
if (is_array($field_data)) { // 如果是多选等情况时转换
$field_data = implode(',', $field_data);
}
$field_data = str_replace('pboot:if', '', $field_data);
if ($value->required && ! $field_data) {
alert_back($value->description . '不能为空!');
} else {
$data[$value->name] = $field_data;
$mail_body .= $value->description . ':' . $field_data . '
';
}
}
// 设置创建时间
if ($data) {
$data['create_time'] = get_datetime();
}
// 写⼊数据
if ($this->model->addForm($value->table_name, $data)) { session('lastsub', time()); // 记录最后提交时间$this->log('提交表单数据成功!');
if ($this->config('form_send_mail') && $this->config('message_send_to')) {
$mail_subject = "【PbootCMS】您有新的" . $value->form_name . "信息,请注意查收!";
$mail_body .= '
来⾃⽹站 ' . get_http_url() . ' (' . date('Y-m-d H:i:s') . ')';
sendmail($this->config(), $this->config('message_send_to'), $mail_subject, $mail_body);
}
alert_location('提交成功!', '-1', 1);
} else {
$this->log('提交表单数据失败!');
alert_back('提交失败!');

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。