<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>
data-appid : APPID
data-cbfn : 回调函数名
id=‘TencentCaptcha‘ 这个我是默认使用的。
// 腾讯云极验验证码
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("手机号不能为空");
}
});
下面是一个公共方法
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];
}
}
原文:https://www.cnblogs.com/zhenzi0322/p/12857198.html