標籤:send logger tsm and result 代碼 success top 開發
1.登入平台 阿里大於
2.登陸之後我們可以看到資費,使用情境等,在進入正題之前我們需要一些準備工作,首先我們先瞭解下簡訊的請求參數,在這裡我們需要注意的是sms_param這個參數,在接下來我們申請簡訊模板的時候會用到
3.相應參數,對待傳回值我們記住最基本的true or false 即可
4.瞭解完公用參數,接下來我們進入正題,首先我們得配置簡訊簽名和配置簡訊模板,簡訊簽名出現在簡訊開頭,注意簡訊模板,這裡將用到上文提示的sms_param參數
5.同時,在其網站上下載好SDK,展示的是我的存放位置,使用的是thinkphp架構,其他的開發環境檔案的存放位置在官網上有詳細說明
6.接下來是項目背景配置,其中appkey,secret在你完成上述申請之後可以在個人管理中心查看
class DuanXinController extends Controller {
public function index()
{
$appkey = "...";//你的App key
$secret = "...";//你的App Secret:
import(‘Org.taobao.top.TopClient‘);
import(‘Org.taobao.top.ResultSet‘);
import(‘Org.taobao.top.RequestCheckUtil‘);
import(‘Org.taobao.top.TopLogger‘);
import(‘Org.taobao.top.request.AlibabaAliqinFcSmsNumSendRequest‘);
//將需要的類引入,並且將檔案名稱改為原檔案名稱.class.php的形式
$c = new \TopClient;
$c->appkey = $appkey;
$c->secretKey = $secret;
$req = new \AlibabaAliqinFcSmsNumSendRequest;
$req->setExtend("123456");//確定發給的是哪個使用者,參數為使用者id
$req->setSmsType("normal");
session_start();
$verifycode = strval(rand(1000,9999));
$_SESSION[‘verifycode‘] = $verifycode;
$userStatus=0;
/*
進入阿里大魚的管理中心找到簡訊簽名管理,輸入已存在簽名的名稱,這裡是身分識別驗證。
*/
$req->setSmsFreeSignName("山水優品");
$smsParams = [
‘code‘ => $verifycode,
‘product‘ => ‘...‘
];
$req->setSmsParam(json_encode($smsParams));
//$req->setSmsParam("{‘code‘:numfour() ‘product‘:‘山水優品‘}");
//這裡設定的是發送的簡訊內容:驗證碼${code},您進行中${product}身分識別驗證,打死不要告訴別人哦!”
$req->setRecNum(...);//參數為使用者的手機號碼
$req->setSmsTemplateCode("SMS_16670740");
$resp = $c->execute($req);
//var_dump($resp);這裡是用來列印返回結果
if($resp->result->success)
{
$userStatus=1;
}
else
{
$userStatus=0;
}
echo $userStatus;
}
// 檢查驗證碼是否正確
function checkcode(){
session_start();
$verifycode = $_SESSION[‘verifycode‘];
$inputcode = I(‘post.code‘);
$checkstatus = 0;
if ($inputcode == $verifycode) {
$checkstatus = 1;
}else{
$checkstatus = 0;
}
echo $checkstatus;
}
7.下面的代碼是項目前台的Ajax請求部分:
send.onclick = function() {
var oldTel = document.getElementById(‘oldTel‘).value;
var that = this;
var times = 60;
this.disabled = true;
timer1 = setInterval(function() {
times--;
that.value = times + "秒後重試";
if (times <= 0) {
that.disabled = false;
that.value = "發送驗證碼";
clearInterval(timer1);
times = 60;
}
}, 1000);
$.ajax({
url: "{:U(‘DuanXin/index‘)}",
type: "post",
data: {
‘name‘:oldTel,
},
success: function(responseText, status, xhr) {
if (status == ‘success‘) {
if (responseText == 1) {
return true;
} else {
return false;
}
} else {
return false;
}
},
error: function() {
return false;
},
timeout: 1000 * 60,
});
}
這裡的按鈕加入了一個簡單的倒計時功能,即點擊發送後60s內無法點擊按鈕;
阿里大於驗證碼發送 (ThinkPhp架構)