zoukankan      html  css  js  c++  java
  • 阿里云发送短信

    一、首先注册阿里云账号

    二、开通短息服务

    三、点击管理控制台

    四、进行实名认证,然后点击【国内短信】,申请签名和模板

    五、点击【帮助文档】,上面有详细讲解怎样接入短信的完整流程

    六、php实现代码

    首先要使用composer安装阿里云短信依赖

    composer require alibabacloud/client

    <?php
    
    
    namespace appindexcontroller;
    
    
    use thinkController;
    use AlibabaCloudClientAlibabaCloud;
    use AlibabaCloudClientExceptionClientException;
    use AlibabaCloudClientExceptionServerException;
    use thinkException;
    
    
    class Message extends Controller
    {
        private $config;
    
        /**
         * 发送短信
         *
         * @return 	hink
    esponseJson
         * @throws ClientException
         * @throws Exception
         * @throws 	hinkexceptionPDOException
         */
        public function send()
        {
            if (!request()->isPost()) {
                return tips('非法请求');
            }
            $method = input('_method', '', 'trim');
            if (strtoupper($method) != "POST") {
                return tips('非法请求');
            }
    
            $mobile = input('mobile', '', 'trim');
            try {
            $this->config = config('ali_sms');
    
            AlibabaCloud::accessKeyClient($this->config['accessKeyId'], $this->config['accessKeySecret'])
                ->regionId('cn-hangzhou')
                ->asDefaultClient();
    
            
                $code = rand(100000, 999999);
                $data = [
                    'fcontent'  => $code,
                    'fmobile'   => $mobile,
                    'ftype'     => 0,
                    'fstatus'   => 1,
                    'fadd_time' => time()
                ];
    
                $id = db('mobile_apply')->insert($data);
    
                $result = AlibabaCloud::rpc()
                    ->product('Dysmsapi')
                    ->version('2017-05-25')
                    ->action('SendSms')
                    ->method('POST')
                    ->options([
                        'query' => [
                            'PhoneNumbers'  => $mobile,
                            'SignName'      => $this->config['signName'],
                            'TemplateCode'  => $this->config['templateCodeValidate'],
                            'TemplateParam' => json_encode(['code' => $code]),
                        ],
                    ])
                    ->request();
                $res    = $result->toArray();
    
                db('mobile_apply')->where('fstatus', $id)
                    ->update(['fstatus' => 2, 'fmsg' => $result->toJson()]);
    
                if ($res['Code'] != "OK") {
                    throw new Exception($result['Message']);
                }
    
                return tips('发送成功', 1, $res);
            } catch (ClientException $e) {
                return tips($e->getErrorMessage());
            } catch (ServerException $e) {
                return tips($e->getErrorMessage());
            } catch (Exception $e) {
                return tips($e->getMessage());
            }
        }
    
        /**
         * 验证短信
         *
         * @return array|string|	hink
    esponseJson|true
         * @throws 	hinkdbexceptionDataNotFoundException
         * @throws 	hinkdbexceptionModelNotFoundException
         * @throws 	hinkexceptionDbException
         */
        public function valid()
        {
            if (request()->isPost()) {
                try {
                    $mobile = input('mobile', '', 'trim');
                    $code   = input('code', '', 'trim');
                    $fid   = input('fid', '', 'trim');
    
                    $this->config = config('ali_sms');
                    $sms          = db('mobile_apply')
                        ->where(['fmobile' => $mobile, 'ftype' => ['eq', 0], 'fstatus' => 2])
                        ->order('fadd_time desc')
                        ->find();
    
                    if (!$sms) {
                        throw new Exception('短信验证码已过期');
                    }
                    if ((time() - $sms['fadd_time']) > $this->config['expireTime']) {
                        throw new Exception('短信验证码已经过期');
                    }
    
                    if ($code == $sms['fcontent']) {
                        db('mobile_apply')->where(['fmobile' => $mobile, 'ftype' => ['eq', 0]])->delete();
                        db('account')->where(['fid'=>$fid])->update(['faccount_mobile'=>$mobile]);
                        return tips('验证成功', 1);
                    } else {
                        return tips('验证失败');
                    }
                } catch (Exception $e) {
                    return tips($e->getMessage());
                }
            } else {
                return tips('非法请求');
            }
        }
    
    }

    配置文档

    <?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: liu21st <liu21st@gmail.com>
    // +----------------------------------------------------------------------
    
    return [
    'ali_sms'=>[
            'accessKeyId'=>'lalalalalall',//阿里云后台申请
            'accessKeySecret'=>'hahahahahahhah',//阿里云后台申请
            'expireTime'=>600, // 5分钟 根据短信模板内容调整
            'signName'=>'test商城', // 短信签名 []中内容 阿里云后台添加
            'templateCodeValidate'=>'SMS_169897105' //短信模板 阿里云后台添加
        ],
    ];
  • 相关阅读:
    7-[CSS]-css介绍,引入方式
    6-[HTML]-标签属性
    5-[HTML]-body常用标签2
    4-[HTML]-body常用标签1
    3-[HTML]-head标签介绍
    2-[HTML]--介绍
    1-[前端]--前端内容介绍
    35
    33 -jQuery 属性操作,文档操作(未完成)
    1o xiaomi
  • 原文地址:https://www.cnblogs.com/ivy-zheng/p/11389547.html
Copyright © 2011-2022 走看看