laravel 容联云发送短信

一. 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,
        ],
        //...
    ],
];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值