【问题标题】:Why No response from Google Invisible recaptcha?为什么 Google Invisible recaptcha 没有回应?
【发布时间】:2018-06-14 04:31:25
【问题描述】:

尝试实现谷歌隐形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(验证recaptcha)

<?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 错误:检查以确保您的密钥与注册的域匹配并且位于正确的位置。您可能还需要仔细检查您的代码是否存在拼写错误或语法错误。

【问题讨论】:

    标签: php recaptcha invisible-recaptcha


    【解决方案1】:

    请试试这个

           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;
        }
    

    【讨论】:

    • 看起来它对我有用。一旦我将其应用于我的问题,将其标记为 ANSWER。
    【解决方案2】:

    确保仔细检查代码是否存在语法错误。您缺少站点密钥周围的左引号。您还有一个额外的括号,其中显示 json_decode。

    最后,如果你要使用这个条件,你不需要把它分成两个PHP文件:

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

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

    【讨论】:

    • 没关系。在问题形成过程中遗漏了单引号,制作单独的文件或制作单个文件并不能解决我的问题。修正了问题中的拼写错误。
    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 2017-11-16
    • 1970-01-01
    相关资源
    最近更新 更多