溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用PHP怎么實(shí)現(xiàn)一個(gè)短信驗(yàn)證碼接口

發(fā)布時(shí)間:2021-02-03 16:17:23 來(lái)源:億速云 閱讀:158 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)利用PHP怎么實(shí)現(xiàn)一個(gè)短信驗(yàn)證碼接口,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

步驟:

1、登錄榮聯(lián)運(yùn)通訊注冊(cè)獲取ACCOUNT SID、AUTH TOKEN、Rest URL(生產(chǎn))、AppID(默認(rèn));

2、注冊(cè)測(cè)試用手機(jī)號(hào)碼(先注冊(cè)測(cè)試號(hào)碼方可使用);

3、下載demo示例,并將代碼放到項(xiàng)目中(最好單獨(dú)建文件夾存儲(chǔ))。

代碼區(qū):

一、新建test.app.php(測(cè)試用控制器)

<?php
/*
* 短信接口測(cè)試
*/
class TestApp extends ShoppingbaseApp{
public function index(){
$this->display('test.html');
}
public function get_info()
{
include_once(ROOT_PATH.'/includes/rly_message/rest.app.php');//引入模板文件
//主帳號(hào),對(duì)應(yīng)開(kāi)官網(wǎng)發(fā)者主賬號(hào)下的 ACCOUNT SID
$accountSid = '';
//主帳號(hào)令牌,對(duì)應(yīng)官網(wǎng)開(kāi)發(fā)者主賬號(hào)下的 AUTH TOKEN
$accountToken = '';
//應(yīng)用Id,在官網(wǎng)應(yīng)用列表中點(diǎn)擊應(yīng)用,對(duì)應(yīng)應(yīng)用詳情中的APP ID
//在開(kāi)發(fā)調(diào)試的時(shí)候,可以使用官網(wǎng)自動(dòng)為您分配的測(cè)試Demo的APP ID
$appId ='';
//請(qǐng)求地址
//沙盒環(huán)境(用于應(yīng)用開(kāi)發(fā)調(diào)試):sandboxapp.cloopen.com
//生產(chǎn)環(huán)境(用戶應(yīng)用上線使用):app.cloopen.com
$serverIP ='app.cloopen.com';
//請(qǐng)求端口,生產(chǎn)環(huán)境和沙盒環(huán)境一致
$serverPort ='8883';
//REST版本號(hào),在官網(wǎng)文檔REST介紹中獲得。
$softVersion ='2013-12-26';
//global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
$phone_mob = isset($_GET['phone_mob']) ? $_GET['phone_mob'] : '';
if($phone_mob){
$this->sendTemplateSMS($phone_mob,array('3519','5'),"1",$accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion);//手機(jī)號(hào)碼,替換內(nèi)容數(shù)組,模板ID
}
}
/**
* 發(fā)送模板短信
* @param to 手機(jī)號(hào)碼集合,用英文逗號(hào)分開(kāi)
* @param datas 內(nèi)容數(shù)據(jù) 格式為數(shù)組 例如:array('Marry','Alon'),如不需替換請(qǐng)?zhí)?nbsp;null
* @param $tempId 模板Id,測(cè)試應(yīng)用和未上線應(yīng)用使用測(cè)試模板請(qǐng)?zhí)顚?xiě)1,正式應(yīng)用上線后填寫(xiě)已申請(qǐng)審核通過(guò)的模板ID
*/ 
public function sendTemplateSMS($to,$datas,$tempId,$accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion)
{
// 初始化REST SDK
$rest = new RestApp($serverIP,$serverPort,$softVersion);
$rest->setAccount($accountSid,$accountToken);
$rest->setAppId($appId);
// 發(fā)送模板短信
echo "Sending TemplateSMS to $to <br/>";
$result = $rest->sendTemplateSMS($to,$datas,$tempId);
if($result == NULL ) {
echo "result error!";
exit();
}
if($result->statusCode!=0) {
echo "error code :" . $result->statusCode . "<br>";
echo "error msg :" . $result->statusMsg . "<br>";
//TODO 添加錯(cuò)誤處理邏輯
}else{
echo "Sendind TemplateSMS success!<br/>";
// 獲取返回信息
$smsmessage = $result->TemplateSMS;
echo "dateCreated:".$smsmessage->dateCreated."<br/>";
echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
//TODO 添加成功處理邏輯
}
}
}

二、模板文件(rest.app.php可根據(jù)自己使用的框架重新命名,也可直接使用demo原先的名字)

<?php
class RestApp {
private $AccountSid ; 
private $AccountToken ; 
private $AppId ; 
private $ServerIP; // ='app.cloopen.com'
private $ServerPort ; //='8883'
private $SoftVersion ; // ='2013-12-26'
private $Batch; //時(shí)間戳
private $BodyType = "xml";//包體格式,可填值:json 、xml
private $enabeLog = true; //日志開(kāi)關(guān)??商钪担簍rue、
private $Filename="./log.txt"; //日志文件
private $Handle; 
function __construct($ServerIP,$ServerPort,$SoftVersion) 
{
$this->RestApp($ServerIP,$ServerPort,$SoftVersion);
}
function RestApp($ServerIP,$ServerPort,$SoftVersion)
{
$this->Batch = date("YmdHis");
$this->ServerIP = $ServerIP;
$this->ServerPort = $ServerPort;
$this->SoftVersion = $SoftVersion;
$this->Handle = fopen($this->Filename, 'a');
}
/**
* 設(shè)置主帳號(hào)
* 
* @param AccountSid 主帳號(hào)
* @param AccountToken 主帳號(hào)Token
*/ 
function setAccount($AccountSid,$AccountToken){
$this->AccountSid = $AccountSid;
$this->AccountToken = $AccountToken; 
}
/**
* 設(shè)置應(yīng)用ID
* @param AppId 應(yīng)用ID
*/
function setAppId($AppId){
$this->AppId = $AppId; 
}
/**
* 打印日志
* 
* @param log 日志內(nèi)容
*/
function showlog($log){
if($this->enabeLog){
fwrite($this->Handle,$log."\n"); 
}
}
/**
* 發(fā)起HTTPS請(qǐng)求
*/
function curl_post($url,$data,$header,$post=1)
{
//初始化curl
$ch = curl_init();
//參數(shù)設(shè)置 
$res= curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, $post);
if($post)
{
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
$result = curl_exec($ch);
//連接失敗
if($result == FALSE){
if($this->BodyType=='json'){
$result = "{\"statusCode\":\"172001\",\"statusMsg\":\"網(wǎng)絡(luò)錯(cuò)誤\"}";
} else {
$result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>網(wǎng)絡(luò)錯(cuò)誤</statusMsg></Response>"; 
} 
}
curl_close($ch);
return $result;
} 
/**
* 發(fā)送模板短信
* @param to 短信接收彿手機(jī)號(hào)碼集合,用英文逗號(hào)分開(kāi)
* @param datas 內(nèi)容數(shù)據(jù)
* @param $tempId 模板Id
*/ 
function sendTemplateSMS($to,$datas,$tempId)
{
//主帳號(hào)鑒權(quán)信息驗(yàn)證,對(duì)必選參數(shù)進(jìn)行判空。
//$auth=$this->accAuth();
// if($auth!=""){
// return $auth;
// }
// 拼接請(qǐng)求包體
if($this->BodyType=="json"){
$data="";
for($i=0;$i<count($datas);$i++){
$data = $data. "'".$datas[$i]."',"; 
}
$body= "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[".$data."]}";
}else{
$data="";
for($i=0;$i<count($datas);$i++){
$data = $data. "<data>".$datas[$i]."</data>"; 
}
$body="<TemplateSMS>
<to>$to</to> 
<appId>$this->AppId</appId>
<templateId>$tempId</templateId>
<datas>".$data."</datas>
</TemplateSMS>";
}
$this->showlog("request body = ".$body);
// 大寫(xiě)的sig參數(shù) 
$sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
// 生成請(qǐng)求URL 
$url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
$this->showlog("request url = ".$url);
// 生成授權(quán):主帳戶Id + 英文冒號(hào) + 時(shí)間戳。
$authen = base64_encode($this->AccountSid . ":" . $this->Batch);
// 生成包頭 
$header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
// 發(fā)送請(qǐng)求
$result = $this->curl_post($url,$body,$header);
$this->showlog("response body = ".$result);
if($this->BodyType=="json"){//JSON格式
$datas=json_decode($result); 
}else{ //xml格式
$datas = simplexml_load_string(trim($result," \t\n\r"));
}
// if($datas == FALSE){
// $datas = new stdClass();
// $datas->statusCode = '172003';
// $datas->statusMsg = '返回包體錯(cuò)誤'; 
// }
//重新裝填數(shù)據(jù)
if($datas->statusCode==0){
if($this->BodyType=="json"){
$datas->TemplateSMS =$datas->templateSMS;
unset($datas->templateSMS); 
}
}
return $datas; 
} 
/**
* 主帳號(hào)鑒權(quán)
*/ 
function accAuth()
{
if($this->ServerIP==""){
$data = new stdClass();
$data->statusCode = '172004';
$data->statusMsg = 'IP為空';
return $data;
}
if($this->ServerPort<=0){
$data = new stdClass();
$data->statusCode = '172005';
$data->statusMsg = '端口錯(cuò)誤(小于等于0)';
return $data;
}
if($this->SoftVersion==""){
$data = new stdClass();
$data->statusCode = '172013';
$data->statusMsg = '版本號(hào)為空';
return $data;
} 
if($this->AccountSid==""){
$data = new stdClass();
$data->statusCode = '172006';
$data->statusMsg = '主帳號(hào)為空';
return $data;
}
if($this->AccountToken==""){
$data = new stdClass();
$data->statusCode = '172007';
$data->statusMsg = '主帳號(hào)令牌為空';
return $data;
}
if($this->AppId==""){
$data = new stdClass();
$data->statusCode = '172012';
$data->statusMsg = '應(yīng)用ID為空';
return $data;
} 
}
}
?>

三、前臺(tái)模板代碼(test.html)

<div >
<form action="" method="post">
請(qǐng)輸入手機(jī)號(hào)碼:<input type="text" name="phone_mob" ><input type="button" name="btn" value="點(diǎn)擊獲取驗(yàn)證碼"><br />
輸入驗(yàn)證碼:<input type="text" name="yzm" value=""><br />
<input type="submit" name="sbt" value="提交">
</form>
</div>

四、js代碼

<script type="text/javascript">
$(function(){
$('input[name=btn]').on('click',function(){
var phone_mob = $('input[name=phone_mob]').val();
$.ajax({
type: "GET",
url: "index.php?app=test&act=get_info",//請(qǐng)求后臺(tái)的URL地址
dataType: "json",
data:{'phone_mob' : phone_mob},
success:function(data){
if(data.done && data.retval)
{ 
//console.log(data.retval);
alert('成功!');
}
}
});
}); 
});
</script>

以上就是利用PHP怎么實(shí)現(xiàn)一個(gè)短信驗(yàn)證碼接口,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI