猫小怪

世人慌慌张张,不过图碎银几两。

 

tp3 对接短信接口

前端

  html

  <div>
      <input type="text" id="phone" name="shouji" placeholder="请输入手机号">
      <input type="button" style="float: right;color: #fff;width:40%;border-radius: 0 5px 5px 0" onclick="settime(this)" value="获取验证码">
      <!--<input type="button" style="color: #000;border: 1px solid;"  class="yzm" placeholder="获取验证码">获取验证码</input>-->
  </div>

  js

  <script>

    var countdown = 60;

    function settime(obj) {
        if (countdown == 60) {
            jQuery.ajax({
                type: "POST",
                url: "{:U('Center/sendcode')}",
                global: false,
                dataType: "json",
                data: $("#form1").serialize(),
                success: function (result) {
                    // $('#mess').show();
                    // $('#mess').html(result.info);
//                    $('#yanid').val(result.id);
                }
            });
        }
        if (countdown == 0) {
            obj.removeAttribute("disabled");
            obj.value = "点击获取";
            countdown = 60;

            return;
        } else {

            obj.setAttribute("disabled", true);
            obj.value = "" + countdown + "s";
            countdown--;

        }
        setTimeout(function () {
                settime(obj)
            }
            , 1000)
    }
</script>

php

  public function sendcode()
{
    require_once 'Api/api_sdk/vendor/autoload.php';
    Config::load();             //加载区域结点配置
    $ar = rand('100000','999999');
    $code=$ar;
    session('yzm',$code);


    $accessKeyId = "**************";//自己替换自己的accessKeyId
    $accessKeySecret = "****************************************";//自己替换自己的accessKeySecret
    $templateParam = array("code" => $code);           //模板变量替换              假如要用验证码的话 需要把你短信模板的验证码参数改正   如  $templateParam = array("code"=>$code);
    $templateCode = '************';   //短信模板ID
    $signName = "名称";
    //短信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('***********');

    // 必填,设置签名名称
    $request->setSignName($signName);

    // 必填,设置模板CODE
    $request->setTemplateCode($templateCode);

    // 可选,设置模板参数
    if ($templateParam) {
        $request->setTemplateParam(json_encode($templateParam));
    }

    //发起访问请求
    $acsResponse = $acsClient->getAcsResponse($request);

    //返回请求结果
    $result = json_decode(json_encode($acsResponse), true);

    return $result;
}

  

posted on 2020-06-08 21:01  猫小怪  阅读(150)  评论(0编辑  收藏  举报

导航