登入之手機號驗證碼登入
阿新 • • 發佈:2019-01-03
今天測試雲之訊api 雲之訊官網提供原始碼只需要修改就好
我的是放入tp5路徑如下:前端程式碼腦補 只有一個input和按鈕
直接上程式碼
index.php
PhoneSend.php<?php namespace app\admin\controller; use think\Db; use think\Loader; use think\Controller; use app\admin\controller\PhoneSend; class Index extends Controller { public function index() { return $this->fetch(); } public function set(){ // var_dump($_POST); $obj = new PhoneSend(); $obj->text(); } }
<?php namespace app\admin\controller; use app\admin\controller\Ucpass; class PhoneSend{ public function text(){ //初始化必填 //填寫在開發者控制檯首頁上的Account Sid $options['accountsid']='0ac771342e5d4a88985e72777b940287'; //填寫在開發者控制檯首頁上的Auth Token $options['token']='e1cea586fcfb361167de2264d8693f56'; //初始化 $options必填 /*隨機生成6位驗證碼 複雜方式*/ srand((double)microtime()*1000000);//create a random number feed. $authnum; $ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; $list=explode(",",$ychar); for($i=0;$i<6;$i++){ $randnum=rand(0,35); // 10+26; $authnum[$i]=$list[$randnum]; } $authnum = implode('', $authnum); // 簡單的驗證 6位純數字 // $authnum = rand(100000,999999); $ucpass = new \app\admin\controller\Ucpaas($options); $appid = "88d1b0f941f942c9a45fc354653af2ae"; //填寫自己的 $mobile = $_POST['mobile']; $templateid = "313343"; $param=$authnum; $uid=''; $arr=$ucpass->SendSms($appid,$templateid,$param,$mobile,$uid); /*if (substr($arr,21,6) == 000000) { //如果成功就,這裡只是測試樣式,可根據自己的需求進行調節 echo "簡訊驗證碼已傳送成功,請注意查收簡訊"; }else{ //如果不成功 echo "簡訊驗證碼傳送失敗,請聯絡客服"; }*/ } }
Ucpass.php
<?php namespace app\admin\controller; class Ucpaas { //API請求地址 const BaseUrl = "https://open.ucpaas.com/ol/sms/"; //開發者賬號ID。由32個英文字母和阿拉伯數字組成的開發者賬號唯一識別符號。 private $accountSid; //開發者賬號TOKEN private $token; public function __construct($options) { if (is_array($options) && !empty($options)) { $this->accountSid = isset($options['accountsid']) ? $options['accountsid'] : ''; $this->token = isset($options['token']) ? $options['token'] : ''; } else { throw new Exception("非法引數"); } } private function getResult($url, $body = null, $method) { $data = $this->connection($url,$body,$method); if (isset($data) && !empty($data)) { $result = $data; } else { $result = '沒有返回資料'; } return $result; } /** * @param $url 請求連結 * @param $body post資料 * @param $method post或get * @return mixed|string */ private function connection($url, $body,$method) { if (function_exists("curl_init")) { $header = array( 'Accept:application/json', 'Content-Type:application/json;charset=utf-8', ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); if($method == 'post'){ curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$body); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($ch); curl_close($ch); } else { $opts = array(); $opts['http'] = array(); $headers = array( "method" => strtoupper($method), ); $headers[]= 'Accept:application/json'; $headers['header'] = array(); $headers['header'][]= 'Content-Type:application/json;charset=utf-8'; if(!empty($body)) { $headers['header'][]= 'Content-Length:'.strlen($body); $headers['content']= $body; } $opts['http'] = $headers; $result = file_get_contents($url, false, stream_context_create($opts)); } return $result; } /** 單條傳送簡訊的function,適用於註冊/找回密碼/認證/操作提醒等單個使用者單條簡訊的傳送場景 * @param $appid 應用ID * @param $mobile 接收簡訊的手機號碼 * @param $templateid 簡訊模板,可在後臺簡訊產品→選擇接入的應用→簡訊模板-模板ID,檢視該模板ID * @param null $param 變數引數,多個引數使用英文逗號隔開(如:param=“a,b,c”) * @param $uid 用於貴司標識簡訊的引數,按需選填。 * @return mixed|string * @throws Exception */ public function SendSms($appid,$templateid,$param=null,$mobile,$uid){ $url = self::BaseUrl . 'sendsms'; $body_json = array( 'sid'=>$this->accountSid, 'token'=>$this->token, 'appid'=>$appid, 'templateid'=>$templateid, 'param'=>$param, 'mobile'=>$mobile, 'uid'=>$uid, ); $body = json_encode($body_json); $data = $this->getResult($url, $body,'post'); return $data; } /** 群傳送簡訊的function,適用於運營/告警/批量通知等多使用者的傳送場景 * @param $appid 應用ID * @param $mobileList 接收簡訊的手機號碼,多個號碼將用英文逗號隔開,如“18088888888,15055555555,13100000000” * @param $templateid 簡訊模板,可在後臺簡訊產品→選擇接入的應用→簡訊模板-模板ID,檢視該模板ID * @param null $param 變數引數,多個引數使用英文逗號隔開(如:param=“a,b,c”) * @param $uid 用於貴司標識簡訊的引數,按需選填。 * @return mixed|string * @throws Exception */ public function SendSms_Batch($appid,$templateid,$param=null,$mobileList,$uid){ $url = self::BaseUrl . 'sendsms_batch'; $body_json = array( 'sid'=>$this->accountSid, 'token'=>$this->token, 'appid'=>$appid, 'templateid'=>$templateid, 'param'=>$param, 'mobile'=>$mobileList, 'uid'=>$uid, ); $body = json_encode($body_json); $data = $this->getResult($url, $body,'post'); return $data; } }