任何人都可以帮助我实施Google reCaptchaV3。
以下是我尝试的东西
<?php
// Check if form was submitted:
echo '<pre>';
print_r($_POST);
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recaptcha_response'])) {
// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'MY-Recaptcha-secret-key-v3';
$recaptcha_response = $_POST['recaptcha_response'];
// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);
print_r($recaptcha);
// Take action based on the score returned:
if ($recaptcha->score >= 0.5) {
// Verified - send email
} else {
// Not verified - show form error
}
}
echo '</pre>';
?>
这是我尝试过的html和js代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Google reCAPTCHA v3</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css">
<script src="https://www.google.com/recaptcha/api.js?render=MY-Recaptcha-site-key-v3"></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('MY-Recaptcha-site-key-v3', {action: ''}).then(function (token) {
var recaptchaResponse = document.getElementById('recaptchaResponse');
recaptchaResponse.value = token;
});
});
</script>
</head>
<body>
<section class="section">
<div class="container">
<div class="columns">
<div class="column is-half">
<form method="POST">
<h1 class="title">
reCAPTCHA v3 example
</h1>
<div class="field">
<label class="label">Name</label>
<div class="control">
<input type="text" name="name" class="input" placeholder="Name" required>
</div>
</div>
<div class="field">
<label class="label">Email</label>
<div class="control">
<input type="email" name="email" class="input" placeholder="Email Address" required>
</div>
</div>
<div class="field">
<label class="label">Message</label>
<div class="control">
<textarea name="message" class="textarea" placeholder="Message" required></textarea>
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button type="submit" class="button is-link">Send Message</button>
</div>
</div>
<input type="hidden" name="recaptcha_response" id="recaptchaResponse">
</form>
</div>
</div>
</div>
</section>
</body>
</html>
但是在验证Recaptcha时得到以下响应:
stdClass Object
(
[success] =>
[error-codes] => Array
(
[0] => invalid-input-secret
)
)
请指导我解决它。我从下面拿走了 https://developers.google.com/recaptcha/docs/v3 和 https://github.com/stevie-c91/Google-reCAPTCHA-v3-example/blob/master/public/index.php