網站註冊登錄等短信驗證碼
標籤: 短信驗證碼手機
現在隨着時代的發展,許多網站的註冊,或者登錄都和手機號碼關聯在一起,這樣也方便了我們記住賬號,下面我就來說一下怎麼用PHP發短信,這個是卸載(lavarel)框架的。
這裏給大家介紹一個平臺–雲信使,註冊後大家可以獲得免費短信10條,(這裏告訴大家個祕密,如果不夠用還可以和客服溝通在要幾條)。雲信使:http://www.sms.cn/
1、登錄註冊雲信使賬號,註冊後,點擊導航欄短信設置
2、點擊後我們要先添加短信模板
3、創建模板並通過驗證後,點擊接口發送
4、複製接口格式留着我們一會用
5、我們進入laravel框架寫一個頁面
[html] view plain copy
<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)
[php] view plain copy
//訪問頁面
Route::any('message','MessageController@index');
//發短信的路由
Route::any('message_do','MessageController@message_do');
7、寫好我們的控制器我這裏是MessageController.php
[php] view plain copy
<?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;
}
}