稍等片刻,正在加载中...

腾讯云极验验证码(防火墙验证码)

一. 官网文档

腾讯防火墙

appid与appSecret配置信息获取

二. 实现效果

暂无图片

三. php代码实现

3.1. 前端引入相应的信息

<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>

<input type="text" name="phone" class="phone_text" maxlength="16" placeholder="手机号码">
<button id="TencentCaptcha" class="send_code" data-appid="20xxxxxx" data-cbfn="Tcode" type="button">获取短信码</button>

3.1.1. 解释属性

data-appid : APPID
data-cbfn : 回调函数名
id='TencentCaptcha' 这个我是默认使用的。

3.2. 回调函数js实现

// 腾讯云极验验证码
var Tcode = new TencentCaptcha('20xxx这是appid', function (res) {
    let phone = $('.phone_text').val();
    let data = {'ticket': res.ticket, 'randstr': res.randstr, 'phone': phone};
    $.ajax({
        type: 'POST',
        url: 'url地扯',
        data: data,
        dataType: 'json',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (res) {
            console.log(res);
        }
    });
});
// 点击
$(document).on('click', '#TencentCaptcha', function () {
    let phone = $('.phone_text').val();
    if (phone) {
        Tcode.show(); //显示拼图验证码出来
    }else{
        // $('.error_text').text('手机号不能为空');
        alert("手机号不能为空");
    }
});

3.3. PHP之Laravel框架实现

下面是一个公共方法

use GuzzleHttp\Client; //使用这个门面

public function tencentCode($ticket, $randstr, $UserIp)
    {
        $aid = '20xxx';
        $AppSecretKey = '0yxxxxxxxxx**';
        $Randstr = $randstr; // 随机字符串
        $Ticket = $ticket; // 票据
        $UserIP = $UserIp;
        $client = new Client();
        try {
            $repose = $client->get("https://ssl.captcha.qq.com/ticket/verify?aid=" . $aid . "&AppSecretKey=" . $AppSecretKey . "&Ticket=" . $Ticket . "&Randstr=" . $Randstr . "&UserIP=" . $UserIP);
            $res = json_decode($repose->getBody()->getContents(), true);
            return ['status' => $res['response'], 'err_msg' => $res['err_msg']];
        } catch (RequestException $exception) {
            return ['status' => 0, 'err_mg' => $exception];
        }
    }
posted @ 2020-05-09 14:21  一切皆往事  阅读(567)  评论(0)    收藏  举报