阿里云短信发送

文章目录

  • ​​阿里云短信发送​​
  • ​​一、获取阿里云AccessKey​​
  • ​​二、使用步骤​​
  • ​​1.安装插件[ibrand/laravel-sms](https://github.com/guojiangclub/laravel-sms)​​
  • ​​2.使用​​
  • ​​发送验证码​​
  • ​​验证验证码​​
  • ​​实例代码​​


提示:以下是本篇文章正文内容,下面案例可供参考

一、获取阿里云AccessKey

阿里云帮助中心文档​​link​

二、使用步骤

1.安装插件​​ibrand/laravel-sms​

安装:

composer require ibrand/laravel-sms:~1.0 -vvv

加载资源文件:

php artisan vendor:publish --provider="iBrand\Sms\ServiceProvider"

基础配置如下:

// 可用的网关配置
'gateways' => [
'errorlog' => [
'file' => '/tmp/easy-sms.log',
],
'yunpian' => [
'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
],
'aliyun' => [
'access_key_id' => 'dalvTXXX',
'access_key_secret' => 'XXXX',
'sign_name' => '阿里云短信测试专用',
'code_template_id' => 'SMS_80215252'
],
'alidayu' =>
//...
],
],

2.使用

发送验证码

use iBrand\Sms\Facade as Sms;
$code = Sms::getNewCode(request('mobile'));
$message = new CustomMessage($code->code);

Sms::send(request('mobile'), $message, ['yuntongxun']);

验证验证码

use iBrand\Sms\Facade as Sms;

if (!Sms::checkCode(\request('mobile'), \request('code'))) {
//Add you code.
}

实例代码

<?php
namespace App\Services;

use iBrand\Sms\Facade as Sms;

class AliSms
{
public function __construct()
{

}

static public function send($mobile,$event,$data,$user_sn = null)
{
switch ($event)
{
/**
* 短信验证码
* $data = ['code'=>'验证码'];
*/
case 'code':
$template = 'SMS_209821965';
break;
default:
$template = null;
}
if($template)
{
if(!$mobile)
{
return false;
}
try {
return Sms::send($mobile, ['template' => $template, 'data'=>$data], ['aliyun']);
} catch(\Exception $e) {
return $e->getMessage();
}
}
return false;
}
}