reCAPTCHA 表单返回错误

reCAPTCHA form is returning an error

我的表单中包含一个简单的 Recaptcha,它给我这个错误:Use of undefined constant success - assumed 'success'

我查看了文档,并包含了集成的所有必要组件。当我提交表单时,我仍然收到同样的错误。

感谢您的帮助! :)

这是我的代码:

PHP:

// reCAPTCHA
$captcha = isset($_POST['g-recaptcha-response']) ? $_POST['g-recaptcha-response'] : "";

if (empty($captcha)) {
    echo "Please check the reCAPTCHA.";
}
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=removed_for_demonstration&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if ($response.success == true) {
    echo "It worked";
}

HTML:

<form action="contact.php" method="post">
    <input name="name" type="text" class="form-control" />
    <div class="g-recaptcha" data-sitekey="removed_for_demonstration"></div>
    <input type="submit" name="contact" value="SUBMIT GAME" />
</form>

试试这个:

改变

if ($response.success == true) {

收件人:

if ($response ['success']) == true) {

Google 的 reCaptcha 响应是一个 JSON 对象。如果您想访问数组中的值,则需要使用 json_decode 解码响应。

$decoded_response = json_decode($response, true)
if ($decoded_response["success"] == true) {
    //Do Stuff
}

编辑:

您还可以访问 OOP 风格的数据:

$decoded_response = json_decode($response)
if ($decoded_response->success) {
    //Do Stuff
}