前端接入文档:绑定一个元素并手动传入场景Id和回调
new TencentCaptcha(element, CaptchaAppId, callback, options);
try {
new TencentCaptcha(
document.getElementById('TencentCaptcha'),
'CaptchaAppId',
callback,
{ bizState: '自定义透传参数' }, // (可选) 业务自定义透传参数, 会在回调函数内获取到 (res.bizState)
);
} catch (error) {
loadErrorCallback();
}
function callback(res) {
/* res(验证成功) = {ret: 0, ticket: "String", randstr: "String"}
res(客户端出现异常错误 仍返回可用票据) = {ret: 0, ticket: "String", randstr: "String", errorCode: Number, errorMessage: "String"}
res(用户主动关闭验证码)= {ret: 2}
*/
if (res.ticket){
// 上传票据 可根据errorCode和errorMessage做特殊处理或统计
}
}
{
"bizState":"test",
"appid":"2093541830",
"ret":0,
"ticket":"t03FexlZVDcQNgy-yz8JTuib4sjI-dagr6DVCsjzMbKtCqnf0M…8hcj-gm5tjZmogOa9FeIUiQwPkLUeRsYhYGbD2Ga2U86gPg**",
"randstr":"@foo"
}
快速入门:
后端:
申请安全凭证: API密钥管理https://console.cloud.tencent.com/cam/capihttps://console.cloud.tencent.com/cam/capi
<?php
require_once './tencentcloud-sdk-php/vendor/autoload.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Captcha\V20190722\CaptchaClient;
use TencentCloud\Captcha\V20190722\Models\DescribeCaptchaResultRequest;
try {
$cred = new Credential("AKIDYu1sSloa4HGyPZT6UdV6QTGo79uOcaHh", "B5i4AzXooZ4HDtJn0xf5E8m9F5fy0nLd");
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("captcha.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new CaptchaClient($cred, "", $clientProfile);
$req = new DescribeCaptchaResultRequest();
$params = array(
"CaptchaType" => 19,
"Ticket" => "t03RNWg-CjuUelYLsjE2O05ebPvHFtqFpLuFwwMQ-35SMo-z0ti6lnix6NMj5Cj_CEBbCcYw9cS7rrYzohK2sdgEhWK5pfpSzsdJ6jAhAplJU0*",
"UserIp" => "1.192.215.34",
"Randstr" => "@GwG",
"CaptchaAppId" => 2093541830,
"AppSecretKey" => "0wJ31ytnuZPY9E0tQlqkodQ**"
);
$req->fromJsonString(json_encode($params));
$resp = $client->DescribeCaptchaResult($req);
print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
echo $e;
}
结果:
{
"CaptchaCode":1,
"CaptchaMsg":"OK",
"EvilLevel":0,
"GetCaptchaTime":0,
"RequestId":"9b291117-f081-4f1c-b1d8-e1beb2e91cad"
}