为什么 Google Invisible recaptcha 没有回应?

Why No response from Google Invisible recaptcha?

试图实现Google不可见的recaptcha,但是验证后我没有收到任何回复。

这是我的代码:

invisible_recaptcha.php(表格)

<!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Recaptcha Demo</title>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script>
        function onSubmit(token) {
            document.getElementById("i-recaptcha").submit();
        }
    </script>
    </head>
    <body> 
        <!-- FORM GOES HERE -->
        <form id='i-recaptcha' action="process_recaptcha.php" method="post">
            <label for="fname">First Name*</label><br>
            <input type="text" name="fname" id="fname" required autofocus><br><br>

            <label for="lname">Last Name*</label><br>
            <input type="text" name="lname" id="lname" required><br><br>

            <label for="email">Email Address*</label><br>
            <input type="email" name="email" id="email" required><br><br>

            <button class="g-recaptcha" data-sitekey="XXXXXXmy_site_keyXXXXXXXXX" data-size="invisible" data-callback="onSubmit">
                Submit
            </button>
        </form>


    </body>
    </html>

process_recaptcha.php(验证验证码)

<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => 'XXXXXX_my_secret_key_XXXXXXXXX',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        //echo $user_response."<br><br><br><br>". $fields_string;exit;
        $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 reCAPTCHA is not properly set up
        echo 'reCAPTCHA error: Check to make sure your keys match the registered domain and are in the correct locations. You may also want to doublecheck your code for typos or syntax errors.';
    } else {
        // If CAPTCHA is successful...

        // Paste mail function or whatever else you want to happen here!
        echo '<br><p>CAPTCHA was completed successfully!</p><br>';
    }
} ?>

它总是给我这样的信息: reCAPTCHA 错误:检查以确保您的密钥与注册域匹配并且位于正确的位置。您可能还想仔细检查您的代码是否有拼写错误或语法错误。

请试试这个

       try {
        //Get google capcha details
        if ($site_details['google_captcha_secret_key'] != '') {
            $site_key = $site_details['google_captcha_secret_key'];
        } else {
            $site_key = GOOGLE_CAPTCHA_SECRET_KEY;
        }
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = ['secret' => $site_key,
            'response' => $captcha,
            'remoteip' => $this->userIpAddress];

        $options = [
            'http' => [
                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                'method' => 'POST',
                'content' => http_build_query($data)
            ]
        ];

        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return json_decode($result)->success;
    } catch (Exception $e) {
        return null;
    }

确保仔细检查代码是否存在语法错误。您缺少站点密钥周围的左引号。你还有一个额外的括号,上面写着 json_decode.

最后,如果您要使用此条件,则无需将其分成两个 PHP 文件:

if ($_SERVER['REQUEST_METHOD'] == 'POST')

所以process_recaptcha.php中的php代码可以嵌入到invisible_recaptcha.php中。然后您必须将表单的操作属性更改为其自身 (invisible_recaptcha.php)。该条件将检查表单是否已提交。如果有,它将处理您的 recaptcha 代码,如果没有,它将跳过它。