1 腾讯云申请账号
2 API文档
腾讯云官方文档:
https://cloud.tencent.com/document/product/382/5976
接口描述:
功能描述
给用户发短信验证码、短信通知,营销短信(内容长度不超过 450 字)。
请求地址:
https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=xxxxx&random=xxxx
请求参数
1 tpl_id 字段需填写审核通过的模板 ID,
假如模版 ID 对应的模板内容为: 您的{1}是{2},请于{3}分钟内填写。如非本人操作,请忽略本短信。 ,则上面请求参数组合后下发的内容为: 【腾讯云】您的验证码是1234,请于4分钟内填写。如非本人操作,请忽略本短信。 。
2 如您有多个短信签名,请将需要的短信签名填入 sign 字段,例如您有 腾讯科技 和 腾讯云 两个签名,但想以 腾讯云 签名发送短信,则 sign 字段可赋值为: 腾讯云 。
第一个申请的国内短信签名会作为默认签名,如果有已经申请通过的签名,当实际发送的签名没有申请或者申请未审批时会被替换为副签,若需要返回签名错误(1012)
3 “sig” 字段根据公式
sha256(appkey=
random&time=
mobile)生成
返回参数
3
3 步骤
(1)增加后台国际短信配置页面和对应控制器页面
Southmart/web/themes/default/profile/notify.html
Southmart/web/source/profile/notify.ctrl.php
(2)注册页面
(注:注册验证码,忘记密码验证码,调用的都是腾讯云短信接口,只是传的参数不同。
Type=reg:注册 forget:忘记密码 check:验证码�)
(2.1)发送注册验证码
//发送短信
public function postSendCode($data){
load()->model('cloud');
$smsSendResult=sms_send($data['nationcode'],$data['mobile'],'reg');
if (is_error($smsSendResult)) {
respon::custom($smsSendResult['errno'], $smsSendResult['message']);
}
respon::success('短信已经发送成功,请注意查收。' . $smsSendResult);
}
/**
* 短信发送接口,统一模板。
* @param $mobile
* @param string $type
* @return array|bool
*/
function sms_send($nationcode,$mobile, $type = 'check'){
global $_W;
$row = pdo_fetch("SELECT `notify`,`international_notify` FROM " . tablename('uni_settings') . " WHERE uniacid = :uniacid", array( ':uniacid' => $_W['uniacid'] ));
$notify = iunserializer($row['notify']);
$international_notify=iunserializer($row['international_notify']);
$notify = isset($notify['sms']) ? $notify['sms'] : array(); $international_notify=isset($international_notify['sms'])?$international_notify['sms']:array();
if (!empty( $notify )||!empty($international_notify)) {
# 随机短信验证码
$randCode = rand(100000, 999999);
$receiverSended = pdo_get('uni_verifycode', array('receiver' => $mobile, 'uniacid' => $_W['uniacid']));
$saveData = array();
if(!empty($receiverSended)){
# 单日短信发送限制
if(!empty($notify['msgcount']) && $receiverSended['total'] >= $notify['msgcount']) {
return error(-1, '很抱歉,该号码今日已达发送短信上限。');
}
$saveData = $receiverSended;
$saveData['verifycode'] = $randCode;
$saveData['createtime'] = TIMESTAMP;
$saveData['total'] = $receiverSended['total'] + 1;
}
# 短信统一模板处理
if ($notify['service']||$international_notify['service']) {
# 方法探测执行。(工厂)
//180531国内短(中国)信还是国际短信
if($nationcode=='86'){
if (!preg_match('/^1\d{10}$/', $mobile)) {
return error(1, '发送短信失败, 原因: 手机号错误.');
}
$template = $notify['template'][$type];
$sms_func = $notify['service'] . '_sms_send';//国内短信
}else{
$sms_func=$international_notify['service'] . '_sms_send';//国际短信
$template = $international_notify['template'][$type];
}
if (function_exists($sms_func)) {
$sendRes = $sms_func($nationcode,$mobile, $template, $notify['account'], $notify['password'],$randCode);
if ($sendRes == true) {
if(!empty($saveData)){
$status = pdo_update('uni_verifycode', $saveData, array('id' => $saveData['id']) );
}else{
$saveData['uniacid'] = $_W['uniacid'];
$saveData['receiver'] = $mobile;
$saveData['verifycode'] = $randCode;
$saveData['total'] = 1;
$saveData['createtime'] = TIMESTAMP;
$status = pdo_insert('uni_verifycode', $saveData);
}
if($status === false){
return error(-1, '短信存储失败,请开发人员检查。');
}
# 删除过期记录
$sql = 'DELETE FROM ' . tablename('uni_verifycode') . ' WHERE `createtime`<' . (TIMESTAMP - 1800);
pdo_query($sql);
return true;
}
return error(-1, "短信发送失败,接口返回内容:{$sendRes}");
}
return error(-1, "不支持的短信接口:{$notify['service']}");
}
return error(-1, "未选择短信接口提供商");
}
return error(-1, '商户未配置短信相关参数,无法使用。');
}
/**短信(指定模板单发短信)
* @param $mobile 接收短信的手机号
* @param string $account
* @param string $password 密钥
* @return array|bool
*/
function qcloud_sms_send($nationcode,$mobile, $template,$appid = '', $appkey = '',$randcode=''){
$smsUrl = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=%s&random=%s';
$template=intval($template);
$smsMsg = [
'tel' => [
'nationcode' => $nationcode,
'mobile' => $mobile
],
'params'=>[$randcode],
'sign'=>"南洋超市",
'sig' => hash("sha256", "appkey=".$appkey."&random=".$randcode."&time=".TIMESTAMP."&mobile=".$mobile, FALSE),
'time' => TIMESTAMP,
'extend' => '',
'ext' => '',
'tpl_id'=>$template
];
load()->func('communication');
$res = ihttp_request(sprintf($smsUrl, $appid, $randcode), json_encode($smsMsg));
$resConteng = @json_decode($res['content'], true);
if ($resConteng['result'] != 0) {
return error(-1, $resConteng['errmsg']);
}
return true;
}