1. 程式人生 > >網站註冊登入等簡訊驗證碼

網站註冊登入等簡訊驗證碼

現在隨著時代的發展,許多網站的註冊,或者登入都和手機號碼關聯在一起,這樣也方便了我們記住賬號,下面我就來說一下怎麼用php發簡訊,這個是解除安裝(lavarel)框架的。

這裡給大家介紹一個平臺--雲信使,註冊後大家可以獲得免費簡訊10條,(這裡告訴大家個祕密,如果不夠用還可以和客服溝通在要幾條)。雲信使:http://www.sms.cn/

1、登入註冊雲信使賬號,註冊後,點選導航欄簡訊設定


2、點選後我們要先新增簡訊模板

3、建立模板並通過驗證後,點選介面傳送


4、複製介面格式留著我們一會用


5、我們進入laravel框架寫一個頁面

<div class="register-main" id="redeemPrizes">
    <ul class="register">
        <li>
            <label>手機號:</label>
            <input class="ipt-box tel-bg" value="" id="regi_mobile" type="text">
        </li>
        <li>
            <label>驗證碼:</label>
            <input class="code" value="六位數字驗證碼" id="validatecode" type="text">
            <input class="code" onclick='duanxin()' value="獲取驗證碼" type="button">
        </li>
    </ul>
    <span id="xin_top_userinfo" sname="http://www.xin.com"><div class="person-wrap" style="top:0px;"><a href="javascript:clear_invalid();show_popup('#popupLogin','#popupLogin%20.closeJs');" id="loginA" class="login">登入</a>/<a href="#" id="regA" rel="nofollow" class="register" >註冊</a></div></span>
    <div class="btn-div">
    </div>
    </div>
    <script src="http://www.haoyunyun.cn/jquery.js"></script>
    <script>
        function duanxin(){
            //獲取手機ID
            var iphone=$("#regi_mobile").val();
            $.ajax({
                url:'message_do',
                data:{'iphone':iphone},
                type:"GET",
                dataType:"Json",
                success:function(msg){
                    if(msg['stat']=='100'){
                        alert('簡訊傳送成功了');
                    }else{
                        alert('簡訊傳送失敗了');
                    }

                }
            });
        }
    </script>
6、寫好laravel的路由(routes.php)
//訪問頁面
Route::any('message','[email protected]');
//發簡訊的路由
Route::any('message_do','[email protected]_do');
7、寫好我們的控制器我這裡是MessageController.php
<?php

namespace App\Http\Controllers;

//use App\Http\Controllers\Controller;
//use Illuminate\Foundation\Auth\ResetsPasswords;

class MessageController extends Controller{
    public function index(){
        return view('message');
    }
    public function message_do(){
        $iphone=$_GET['iphone'];
        $code=rand(1000,9999);
        setcookie('code',$code,time()+600);
        //echo $url
//我們在雲信使上的介面格式
 $url='http://api.sms.cn/sms/?ac=send&uid=雲信使登入使用者名稱&pwd=(剛才複製介面的密碼)&template=384954&mobile='.$iphone.'&content={"code":"142B"}';
        /*$url='http://api.sms.cn/sms/?ac=send&uid=haoyunyun888&pwd=ccd843e373206a246826181ab48ed1ee&template=384859&mobile='.$iphone.'&content={"code":"'.$code.'"}';*/
        $data=array();
        $method='GET';
        $res=$this->curlPost($url,$data,$method);
        echo $res;
    }
    /*curlpost傳值*/
    public function curlPost($url,$data,$method){
        $ch = curl_init();   //1.初始化
        curl_setopt($ch, CURLOPT_URL, $url); //2.請求地址
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.請求方式
        //4.引數如下
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模擬瀏覽器
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解壓內容
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
        if($method=="POST"){//5.post方式的時候新增資料
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);//6.執行
        if (curl_errno($ch)) {//7.如果出錯
            return curl_error($ch);
        }
        curl_close($ch);//8.關閉
        return $tmpInfo;
    }


}
現在我們就可以測試著發簡訊啦!大家趕緊試一下吧!!!成功了 別忘了幫忙分享分享,感謝!