zoukankan      html  css  js  c++  java
  • tp5阿里云短信验证码

    1.登录阿里云账号之后 首先鼠标移动到你的头像上 点击AccessKey之后 创建

    2.找到短信服务这里 申请签名 和 模板

    3.之后找到 帮助文档 -> 下载php SDK

     我是点击的开发指南(旧版)下载的 php SDK  非轻量版的

    下载解压之后extend/alisms 

    之后在appcommon.php下

    贴入代码

    <?php
    use AliyunCoreConfig;
    use AliyunCoreProfileDefaultProfile;
    use AliyunCoreDefaultAcsClient;
    use AliyunApiSmsRequestV20170525SendSmsRequest;
    /**
     * 短信发送
     * @param $to    接收人
     * @param $code   短信验证码
     * @return json
     */
    function send_sms($to, $code){
        require_once '../extend/alisms/api_sdk/vendor/autoload.php';
        Config::load(); //加载区域结点配置
        $accessKeyId = '第一步创建的accessKey';
        $accessKeySecret = '第一步创建的accessKeySecret';
        $templateParam = $code;
        //短信API产品名(短信产品名固定,无需修改)
        $product = "Dysmsapi";
        //短信API产品域名(接口地址固定,无需修改)
        $domain = "dysmsapi.aliyuncs.com";
        //暂时不支持多Region(目前仅支持cn-hangzhou请勿修改)
        $region = "cn-hangzhou";
        // 初始化用户Profile实例
        $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
        // 增加服务结点
        DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
        // 初始化AcsClient用于发起请求
        $acsClient= new DefaultAcsClient($profile);
        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();
        // 必填,设置雉短信接收号码
        $request->setPhoneNumbers($to);
        // 必填,设置签名名称
        $request->setSignName("你的签名名称");
        // 必填,设置模板CODE
        $request->setTemplateCode("你的模板code");
        // 可选,设置模板参数
        if($templateParam) {
            $request->setTemplateParam(json_encode(['code'=>$templateParam]));//由于我的模板里需要传递我的短信验证码
        }
        //发起访问请求
        $acsResponse = $acsClient->getAcsResponse($request);
        //返回请求结果
        $result = json_decode(json_encode($acsResponse),true);
        // 具体返回值参考文档:https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.563.YSe8FK
        return $result;
    }
    ?>
    <?php
    public function code(){
            $code = rand(100000,999999);
            $phone = "填写你的手机号";
            $result = send_sms($phone,$code);
            var_dump($result);
        }
    
    ?> 

    之后测试 就可以了

  • 相关阅读:
    POJ 1095 Trees Made to Order 最详细的解题报告
    Producter and Consumer
    How to use the function of bind
    How to use the functions of apply and call
    Configurate vim tool
    #4713. 方程
    #4709. 树
    #4718. 管理
    #4710. 并
    #4707. 点分治
  • 原文地址:https://www.cnblogs.com/buxin111/p/12600148.html
Copyright © 2011-2022 走看看