一. laravel 容联云发送短信
使用包 overtrue/easy-sms
composer require overtrue/easy-sms
/**
* 发送手机验证码
*/
public function phoneCode(Request $request)
{
$request->validate([
'phone' => 'required|regex:/^1[3-9]\d{9}$/'
]);
//发送验证码到手机配置文件
$config = config('sms');
$easySms = new EasySms($config);
$code = rand(1000, 9999);
//缓存验证码
Cache::put('phone_code'.$request->input('phone'),$code,now()->addMinutes(10));
try{
$easySms->send($request->input('phone'), [
'template' => 1,
'data' => [
$code,
'15'
],
]);
}catch (\Exception $e){
return $e->getExceptions();
}
return $this->response->noContent();
}
将配置文件放在sms.php中
\config\sms.php
<?php
/**
* Created by PhpStorm.
* User: wangkxin@foxmail.com
* Date: 2022/11/23
* Time: 21:25
*/
//发送验证码到手机配置文件
return [
// HTTP 请求的超时时间(秒)
'timeout' => 5.0,
// 默认发送配置
'default' => [
// 网关调用策略,默认:顺序调用
'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
// 默认可用的发送网关
'gateways' => [
'yuntongxun'
],
],
// 可用的网关配置
'gateways' => [
'errorlog' => [
'file' => '/tmp/easy-sms.log',
],
'yuntongxun' => [
'app_id' => env('SMS_APP_ID'),
'account_sid' => env('SMS_ASID'),
'account_token' => env('SMS_ATOKEN'),
'is_sub_account' => false,
],
//...
],
];