现在随着时代的发展,许多网站的注册,或者登录都和手机号码关联在一起,这样也方便了我们记住账号,下面我就来说一下怎么用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" οnclick='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','MessageController@index');
//发短信的路由
Route::any('message_do','MessageController@message_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;
}
}
现在我们就可以测试着发短信啦!大家赶紧试一下吧!!!成功了 别忘了帮忙分享分享,感谢!