當初串接三竹簡訊API的時候遇到了不少坑,所以撰寫這篇文章記錄一下看看能不能幫助到迷途的各位,我寫成function的形式方便各位開發,下面程式碼是有包含簡訊及簡易的手機認證
實作部分分為兩個function:
mxp_mitake_sms_text_webhook()是用來檢查使用者的認證及註冊狀況然後發送簡訊
sms_verify()則是用來驗證認證碼是否正確
邏輯部分:
前端填寫完手機號碼並按發送-> mxp_mitake_sms_text_webhook() ->使用者填寫完驗證碼之後按下驗證-> sms_verify()
//簡訊用fumction
function mxp_mitake_sms_text_webhook() {
global $wpdb;
date_default_timezone_set("Asia/Taipei");
//設定時間
$phonenum=$_POST["phonenum"];
//由前端傳送的手機號碼
if (strpos($phonenum, '09') !== 0 || strlen($phonenum) !== 10) {
echo json_encode(array('error'=>'手機格式錯誤'));
exit();
}
//手機號碼格式檢查
$verifyCode=substr(md5(uniqid(rand(), true)),0,5);
$token_exptime = time()+60*10;//過期時間為10分鐘後
//驗證時間設為十分鐘
$cond = $wpdb->prepare(' AND phone_num = %s ', $phonenum); //記得AND前面要留空
$sql = "SELECT verify_stat FROM {$wpdb->prefix}phoneverify where 1=1";
$sql.=$cond;
$verifyStat=$wpdb->get_var($sql);
if($verifyStat=="T"){
$cond = $wpdb->prepare(' AND 電話 = %s ', $phonenum); //記得AND前面要留空
$sql = "SELECT count(*) FROM {$wpdb->prefix}coserRegister where 1=1";
$sql.=$cond;
$regflag=$wpdb->get_var($sql);
if($regflag!=0){
echo json_encode(array('error'=>'此號碼已註冊'));
exit();
}
//檢查號碼是否已註冊成功
else{
$sql="DELETE from `{$wpdb->prefix}phoneverify` WHERE phone_num = '".$phonenum."'";
$intReturn = $wpdb->query($sql);
}
}
$cond = $wpdb->prepare(' AND phone_num = %s ', $phonenum); //記得AND前面要留空
$sql = "SELECT count(phone_num) FROM {$wpdb->prefix}phoneverify where 1=1";
$sql.=$cond;
$smsCount=$wpdb->get_var($sql);
//檢查是否已發送認證簡訊
if($smsCount>0){
$cond = $wpdb->prepare(' AND phone_num = %s ', $phonenum); //記得AND前面要留空
$sql = "SELECT verify_time FROM {$wpdb->prefix}phoneverify where 1=1";
$sql.=$cond;
$smsTime=$wpdb->get_var($sql);
$nowtime = time();
if((intval($nowtime)-intval($smsTime))<30){
echo json_encode(array('error'=>'簡訊已送出囉!請耐心等待,若30秒後未收到請再發送一次!'));
exit();
}
else{
$sql="UPDATE `{$wpdb->prefix}phoneverify` SET `phone_verify`='".$verifyCode."',`verify_time`= '".$token_exptime."' WHERE phone_num = '".$phonenum."'";
$intReturn = $wpdb->query($sql);
if($intReturn==0){
echo json_encode(array('error'=>'系統出錯囉,請聯絡管理員!'));
exit();
}
}
}
else{
$sql = "INSERT INTO {$wpdb->prefix}phoneverify (`phone_num`, `phone_verify`, `verify_time`)
VALUES ('".$phonenum."','".$verifyCode."','".$token_exptime."')";
$intReturn=$wpdb->query($sql);
if($intReturn==0){
echo json_encode(array('error'=>'系統出錯囉,請聯絡管理員'));
exit();
}
}
$url = '三竹簡訊api網址';
//三竹簡訊api網址,要跟克服申請後才會拿到,好像是不固定的
$url .= '&username=$username';//$username請換成自己的username
$url .= '&password=$password';//$password請換成自己的username
$url .= '&dstaddr='.$phonenum;
$url .= '&smbody='.urlencode('您的認證碼是:'.$verifyCode);
$url .= '&CharsetURL=UTF-8';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
curl_close($curl);
$sms_sec = explode(PHP_EOL,$output);
$sms_stat = explode("=",$sms_sec[2]);
$smsStat=$sms_stat[1];
//回傳的狀態碼
$sql="UPDATE {$wpdb->prefix}phoneverify SET `sms_stat`= '".$smsStat."' WHERE phone_num = '".$phonenum."'";
$intReturn = $wpdb->query($sql);
if((int)$smsStat == 0 || (int)$smsStat==1 || (int)$smsStat == 2 || (int)$smsStat== 4){
echo json_encode(array('success'=>'認證碼已發送至您的手機!'));
exit();
}
else{
echo json_encode(array('error'=>'認證碼未發送,請檢查手機號碼是否填寫正確!'));
exit();
}
}
//認證簡訊用function
function sms_verify(){
global $wpdb;
date_default_timezone_set("Asia/Taipei");
$phonenum=$_POST["phonenum"];
$phoneVerify=$_POST["phoneVerify"];
$cond = $wpdb->prepare(' AND phone_num = %s ', $phonenum); //記得AND前面要留空
$sql = "SELECT * FROM {$wpdb->prefix}phoneverify where 1=1";
$sql.=$cond;
$dbResult = $wpdb->get_results($sql);
if($dbResult){
foreach($dbResult as $value){
$dbphoneVerify=$value->phone_verify;
$verifyTime=$value->verify_time;
}
}
$nowtime = time();
if($dbphoneVerify==$phoneVerify&&intval($nowtime)<=intval($verifyTime)){
$sql="UPDATE `{$wpdb->prefix}phoneverify` SET `verify_stat`= 'T' WHERE phone_num = '".$phonenum."'";
$intReturn = $wpdb->query($sql);
if($intReturn==0){
echo json_encode(array('error'=>'系統出錯囉,請聯絡管理員'));
exit();
}
echo json_encode(array('success'=>'認證成功!'));
exit();
}
else{
echo json_encode(array('error'=>'認證失敗!'));
exit();
}
}
?
如果只是要純粹發送簡訊的話只需要這一段
$url = '三竹簡訊api網址';
//三竹簡訊api網址,要跟客服申請後才會拿到,好像是不固定的
$url .= '&username=$username';//$username請換成自己的username
$url .= '&password=$password';//$password請換成自己的password
$url .= '&dstaddr='.$phonenum;
$url .= '&smbody='.urlencode('您的認證碼是:'.$verifyCode);
$url .= '&CharsetURL=UTF-8';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
curl_close($curl);
$sms_sec = explode(PHP_EOL,$output);
$sms_stat = explode("=",$sms_sec[2]);
$smsStat=$sms_stat[1];
//回傳的狀態碼
if((int)$smsStat == 0 || (int)$smsStat==1 || (int)$smsStat == 2 || (int)$smsStat== 4){
echo json_encode(array('success'=>'認證碼已發送至您的手機!'));
exit();
}
else{
echo json_encode(array('error'=>'認證碼未發送,請檢查手機號碼是否填寫正確!'));
exit();
}