我的 reCaptcha V2“我不是机器人”在服务器端失败。它似乎失败了: post_captcha($_POST['g-recaptcha-response'])
公钥和 pvt 密钥都是正确的,并且位于正确的域中。
我创建 reCAPTCHA 已经 3 个小时了,所以通过系统处理我的域可能需要一些时间?我不知道....
这是 HTML:
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="contact-form.php" method="POST">
<div class="g-recaptcha" data-sitekey="PUBLIC KEY"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
这是PHP:
<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => 'PVT KEY',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
} else {
echo 'success';
}
}
?>
任何帮助将不胜感激!谢谢。
Qyouu