示例
一、安装
安装:composer require mrgoon/aliyun-sms dev-master
二、打开config/app.php,添加配置代码
1、‘providers’ 配置组下添加
Mrgoon\Aliyunsms\AliyunsmsServiceProvider::class,
2、‘aliases’ 配置组下添加
'Aliyunsms'=>Mrgoon\Aliyunsms\ServiceProvider::class,
三、运行composer
composer dump-autoload
四、运行php artisan
php artisan vendor:publish` ` --provider="Mrgoon\Aliyunsms\AliyunsmsServiceProvider"
五、修改config/aliyunsms.php
开始调用
<?php
namespace App\Http\Controllers;
use Mrgoon\Aliyunsms\Facades\Aliyunsms;
class SendController
{
public function send(){
$PhoneNumber = "170****8000"; //手机号
$SignName = "模板签名"; //模板签名
$TemplateCode= "模板code"; //模板CODE
$TemplateParam = [
"code" => rand(100000,999999),
"product" => "dsd"
];
$send = Aliyunsms::sendSms(strval($PhoneNumber), $SignName, $TemplateCode, $TemplateParam);
if ($send->Code == 'OK') {
//发送成功后
//存入数据库或者redis
dump("Success");
} else {
dump("failed");
}
}
/*验证填写的验证码和给该手机发送的验证码是否匹配*/
public function verify(Request $req)
{
//第一步:接收到手机号+验证码
$mobile = $req->input('mobile');
$captcha =$req->input('captcha');
// 检查验证码(可从数据库或者redis中获取,仅供参考)
$redisCaptcha = json_decode(Redis::get($mobile), true);
$now = time();
if ($redisCaptcha['expire'] < $now) {
Redis::del($mobile);
return response()->json(['status' => 204, 'error' => '验证码过期']);
}
if ($redisCaptcha['captcha'] != $captcha) {
return response()->json(['status' => 201, 'error' => '验证码错误']);
}
return response()->json(['status'=>200, 'error'=>'输入的验证码和发送的验证码一致']);
}
}