第一步:在官网注册一个账号并登陆,可以直接使用谷歌账号。网站支持中文,也是顶方便的。
第二步:创建站点,点右上角的站点管理。

然后点添加站点:

依次填写相关信息就可以了。

第四步:站点添加之后,你会获取到一个站点的密钥:

第五步:前端集成。官方有说明文档,非常简单。
<div class="h-captcha" data-sitekey="这里就是你刚才的密钥"></div> <script src="https://js.hcaptcha.com/1/api.js" async defer></script>
第六步:后端验证。以PB为例,在提交留言的控制器中添加代码。
$hcaptcha_response = post('h-captcha-response');
$hcaptcha_secret = '你的密钥,这个密钥不是刚才的站点密钥。请打开:https://dashboard.hcaptcha.com/settings/secrets,如下图生成一个密钥';
if(!$hcaptcha_response){
alert_back('未检测到验证码,请刷新页面后重试。');
}
$verify_url = 'https://hcaptcha.com/siteverify';
$post_fields = http_build_query([
'secret' => $hcaptcha_secret,
'response' => $hcaptcha_response,
'remoteip' => $_SERVER['REMOTE_ADDR']
]);
$ch = curl_init($verify_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if(empty($result) || !isset($result['success']) || $result['success'] !== true){
alert_back('机器人验证失败,请重试。');
}
这样就算完成了。快来试试吧。