Не получен ответ об успехе при реализации reCaptchav3 - PullRequest
3 голосов
/ 28 марта 2019

Может ли кто-нибудь помочь мне с реализацией gC reCaptchaV3.

Ниже приведены мои примеры попыток

<code><?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 '
';?>

и это код 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

...