php利用云片網(wǎng)實(shí)現(xiàn)短信驗(yàn)證碼功能的示例代碼
本文將以php舉例,介紹網(wǎng)頁(yè)短信驗(yàn)證碼功能的實(shí)現(xiàn)。
在眾多的第三方短信服務(wù)商中我選擇了云片網(wǎng)這個(gè)短信服務(wù)商,本文也將盡可能利用最簡(jiǎn)單的方式去幫助廣大開(kāi)發(fā)者解決短信驗(yàn)證碼功能模塊的實(shí)現(xiàn)。
再次之前我也參考了大部分網(wǎng)上的博客等,大多數(shù)都是把云片網(wǎng)的demo原封不動(dòng)搬上去,對(duì)于我這個(gè)前端人員來(lái)說(shuō),根本毫無(wú)頭緒,故此我將細(xì)致的講解如何操作,以及獻(xiàn)上我的源碼。
我的業(yè)務(wù)流程就是通過(guò)點(diǎn)擊發(fā)送驗(yàn)證碼這個(gè)按鈕,觸發(fā)一個(gè)ajax請(qǐng)求事件,將手機(jī)號(hào)發(fā)送到后臺(tái),后臺(tái)生成驗(yàn)證碼發(fā)送到手機(jī)端,并返回這個(gè)驗(yàn)證碼給前臺(tái)進(jìn)行驗(yàn)證碼的驗(yàn)證。
請(qǐng)求的php后端代碼如下
post.php
<?php header("Content-Type:text/html;charset=utf-8"); $apikey = "xxxxxxxxxxxxxxx"; //修改為您的apikey(https://www.yunpian.com)登錄官網(wǎng)后獲取 $mobile =$_POST['mobile']; //獲取傳入的手機(jī)號(hào) // $mobile = "xxxxxxxxxxx"; //請(qǐng)用自己的手機(jī)號(hào)代替 $num = rand(1000,9999); //隨機(jī)產(chǎn)生四位數(shù)字的驗(yàn)證碼 setcookie('shopCode',$num); $text="【蒙羊羊】您的驗(yàn)證碼是".$num."。"; $ch = curl_init(); /* 設(shè)置驗(yàn)證方式 */ 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è)置超時(shí)時(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ā)送模板短信 // 需要對(duì)value進(jìn)行編碼 $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ā)送語(yǔ)音驗(yàn)證碼 // $data=array('code'=>$num,'apikey'=>$apikey,'mobile'=>$mobile); // $json_data =voice_send($ch,$data); // $array = json_decode($json_data,true); // echo $num; // 發(fā)送語(yǔ)音通知,務(wù)必要報(bào)備好模板 /* 模板: 課程#name#在#time#開(kāi)始。 最終發(fā)送結(jié)果: 課程深度學(xué)習(xí)在14:00開(kāi)始 */ $tpl_id = 'xxxxxxx'; //修改為你自己后臺(tái)報(bào)備的模板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 '操作完成沒(méi)有任何錯(cuò)誤'; } } ?>
這個(gè)php后臺(tái)是我在官方提供的demo上進(jìn)行修改的,刪除了語(yǔ)音驗(yàn)證這個(gè)功能,只保留了短信驗(yàn)證,并將返回給前端的數(shù)據(jù)只保留了四位數(shù)字的驗(yàn)證碼,方便前端進(jìn)行驗(yàn)證碼的驗(yàn)證。
官方原demo連接如下···鏈接
index.html
如下代碼是進(jìn)行點(diǎn)擊并發(fā)送ajax請(qǐng)求,將請(qǐng)求的驗(yàn)證碼并保存到localStorage中
$.ajax({ type: "post", url: "post.php", //后臺(tái)代碼文件名 data: { mobile:$('#phone').val()//獲取輸入的手機(jī)號(hào) }, // dataType: "json", success:function(data){ console.log(data); layer.msg('驗(yàn)證碼發(fā)送成功,請(qǐng)注意查收!'); localStorage.setItem('code', JSON.stringify(data)) }, error:function(err){ console.log(err); } });
進(jìn)行驗(yàn)證碼驗(yàn)證
var code = JSON.parse(localStorage.getItem('code')) if($('#code').val() != code ){ layer.msg('驗(yàn)證碼輸入錯(cuò)誤'); return false; }
以上驗(yàn)證碼功能講解完畢,如需源碼請(qǐng)點(diǎn)擊(源碼) 自行下載,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
推薦幾個(gè)開(kāi)源的微信開(kāi)發(fā)項(xiàng)目
這篇文章主要推薦幾個(gè)開(kāi)源的微信開(kāi)發(fā)項(xiàng)目,需要的朋友可以參考下2014-12-12關(guān)于擴(kuò)展 Laravel 默認(rèn) Session 中間件導(dǎo)致的 Session 寫(xiě)入失效問(wèn)題分析
這篇文章主要介紹了關(guān)于擴(kuò)展 Laravel 默認(rèn) Session 中間件導(dǎo)致的 Session 寫(xiě)入失效問(wèn)題分析的相關(guān)資料,需要的朋友可以參考下2016-01-01thinkphp項(xiàng)目如何自定義微信分享描述內(nèi)容
本文主要講述:在thinkphp框架中,如何獲取微信分享接口權(quán)限、如何設(shè)置安全域名、如何修改微信分享標(biāo)題、修改微信分享描述、修改微信分享圖片、如何定制微信分享內(nèi)容2017-02-02php二維數(shù)組排序與默認(rèn)自然排序的方法介紹
本篇文章介紹了,在php中二維數(shù)組排序與默認(rèn)自然排序的方法。需要的朋友參考下2013-04-04thinkPHP批量刪除的實(shí)現(xiàn)方法分析
這篇文章主要介紹了thinkPHP批量刪除的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkPHP實(shí)現(xiàn)批量刪除數(shù)據(jù)的數(shù)據(jù)庫(kù)及模板操作相關(guān)技巧,需要的朋友可以參考下2016-11-11PHP執(zhí)行系統(tǒng)命令函數(shù)實(shí)例講解
這篇文章主要介紹了PHP執(zhí)行系統(tǒng)命令函數(shù)實(shí)例講解,列舉的都是一些常用的函數(shù),有感興趣的同學(xué)可以學(xué)習(xí)下2021-03-03PHP大批量插入數(shù)據(jù)庫(kù)的3種方法和速度對(duì)比
這篇文章主要介紹了PHP大批量插入數(shù)據(jù)庫(kù)的3種方法和速度對(duì)比,3種方法分別使用普通insert語(yǔ)句、insert into語(yǔ)句和事務(wù)提交,需要的朋友可以參考下2014-07-07