主頁 > 知識庫 > php利用云片網(wǎng)實現(xiàn)短信驗證碼功能的示例代碼

php利用云片網(wǎng)實現(xiàn)短信驗證碼功能的示例代碼

熱門標簽:全國各省地圖標注點 隨州銷售電銷機器人公司 百度地圖標注類型是酒店 周口網(wǎng)絡(luò)回撥外呼系統(tǒng) 商丘外呼系統(tǒng)好處 外呼系統(tǒng)人工客服 網(wǎng)絡(luò)電話400申請 福建高頻外呼防封系統(tǒng)哪家好 400電話申請辦理

本文將以php舉例,介紹網(wǎng)頁短信驗證碼功能的實現(xiàn)。

在眾多的第三方短信服務(wù)商中我選擇了云片網(wǎng)這個短信服務(wù)商,本文也將盡可能利用最簡單的方式去幫助廣大開發(fā)者解決短信驗證碼功能模塊的實現(xiàn)。

再次之前我也參考了大部分網(wǎng)上的博客等,大多數(shù)都是把云片網(wǎng)的demo原封不動搬上去,對于我這個前端人員來說,根本毫無頭緒,故此我將細致的講解如何操作,以及獻上我的源碼。

我的業(yè)務(wù)流程就是通過點擊發(fā)送驗證碼這個按鈕,觸發(fā)一個ajax請求事件,將手機號發(fā)送到后臺,后臺生成驗證碼發(fā)送到手機端,并返回這個驗證碼給前臺進行驗證碼的驗證。

請求的php后端代碼如下

post.php

?php
header("Content-Type:text/html;charset=utf-8");
$apikey = "xxxxxxxxxxxxxxx"; //修改為您的apikey(https://www.yunpian.com)登錄官網(wǎng)后獲取
$mobile =$_POST['mobile']; //獲取傳入的手機號
// $mobile = "xxxxxxxxxxx"; //請用自己的手機號代替
$num = rand(1000,9999);   //隨機產(chǎn)生四位數(shù)字的驗證碼
setcookie('shopCode',$num);
$text="【蒙羊羊】您的驗證碼是".$num."。";
$ch = curl_init();

/* 設(shè)置驗證方式 */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
/* 設(shè)置返回結(jié)果為流 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* 設(shè)置超時時間*/
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

/* 設(shè)置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// 取得用戶信息
$json_data = get_user($ch,$apikey);
$array = json_decode($json_data,true);
// echo 'pre>';print_r($array);

// 發(fā)送短信
$data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
$json_data = send($ch,$data);
$array = json_decode($json_data,true);
// echo 'pre>';print_r($array);

// 發(fā)送模板短信
// 需要對value進行編碼
$data = array('tpl_id' => '1', 'tpl_value' => ('#code#').
'='.urlencode($num).
''.urlencode('#company#').
'='.urlencode('蒙羊羊'), 'apikey' => $apikey, 'mobile' => $mobile);
// print_r ($data);
$json_data = tpl_send($ch,$data);
$array = json_decode($json_data,true);


echo $num;


// 發(fā)送語音驗證碼
// $data=array('code'=>$num,'apikey'=>$apikey,'mobile'=>$mobile);
// $json_data =voice_send($ch,$data);
// $array = json_decode($json_data,true);
// echo $num;

// 發(fā)送語音通知,務(wù)必要報備好模板
/* 
模板: 課程#name#在#time#開始。 最終發(fā)送結(jié)果: 課程深度學習在14:00開始
 */

$tpl_id = 'xxxxxxx'; //修改為你自己后臺報備的模板id
$tpl_value = urlencode('#time#').'='.urlencode($num).''.urlencode('#name#').'='.urlencode('蒙羊羊');
$data=array('tpl_id'=>$tpl_id,'tpl_value'=>$tpl_value,'apikey'=>$apikey,'mobile'=>$mobile);
$json_data = notify_send($ch,$data);
$array = json_decode($json_data,true);
// echo $num;


curl_close($ch);

/************************************************************************************/
//獲得賬戶
function get_user($ch,$apikey){
curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}
function send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}
function tpl_send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 
'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}
function voice_send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}
function notify_send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}

function checkErr($result,$error) {
if($result === false)
{
echo 'Curl error: ' . $error;
}
else
{
//echo '操作完成沒有任何錯誤';
}
}

?> 

這個php后臺是我在官方提供的demo上進行修改的,刪除了語音驗證這個功能,只保留了短信驗證,并將返回給前端的數(shù)據(jù)只保留了四位數(shù)字的驗證碼,方便前端進行驗證碼的驗證。

官方原demo連接如下···鏈接

index.html

如下代碼是進行點擊并發(fā)送ajax請求,將請求的驗證碼并保存到localStorage中

 $.ajax({ 
  type: "post", 
  url: "post.php", //后臺代碼文件名 
  data: {
  mobile:$('#phone').val()//獲取輸入的手機號
  }, 
  // dataType: "json", 
  success:function(data){ 
  console.log(data);
  layer.msg('驗證碼發(fā)送成功,請注意查收!');
  localStorage.setItem('code', JSON.stringify(data))
  }, 
  error:function(err){ 
  console.log(err); 
  } 
});  

進行驗證碼驗證

var code = JSON.parse(localStorage.getItem('code'))
if($('#code').val() != code ){
  layer.msg('驗證碼輸入錯誤');
  return false;
 }

以上驗證碼功能講解完畢,如需源碼請點擊(源碼) 自行下載,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • thinkPHP框架實現(xiàn)的短信接口驗證碼功能示例
  • PHP手機短信驗證碼實現(xiàn)流程詳解
  • 阿里云PHP SMS短信服務(wù)驗證碼發(fā)送方法
  • 基于PHP實現(xiàn)短信驗證碼接口(容聯(lián)運通訊)
  • php發(fā)送短信驗證碼完成注冊功能
  • php實現(xiàn)的IMEI限制的短信驗證碼發(fā)送類
  • 基于PHP實現(xiàn)短信驗證碼發(fā)送次數(shù)限制

標簽:六安 佛山 南寧 十堰 樂山 海南 定西 迪慶

巨人網(wǎng)絡(luò)通訊聲明:本文標題《php利用云片網(wǎng)實現(xiàn)短信驗證碼功能的示例代碼》,本文關(guān)鍵詞  php,利用,云,片網(wǎng),實現(xiàn),短信,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php利用云片網(wǎng)實現(xiàn)短信驗證碼功能的示例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于php利用云片網(wǎng)實現(xiàn)短信驗證碼功能的示例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章