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;
}