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 = '******';

$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("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)

// 实例化一个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 标准,+[国家或地区码][手机号]

* 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过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('提交失败!');

}

} else {

alert_back('提交失败,请使用POST方式提交!');

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值