2

在 google recaptcha 网站上进行域注册后,我正在尝试在在线网站上实施 Google Invisible Recaptcha,我遵循了文档,但不起作用。发送表单时,显示警报错误:

Cannot contact reCAPTCHA. Check your connection and try again.

这是我的代码(只有有趣的点),HTML/JS:

<script src='https://www.google.com/recaptcha/api.js?
onload=onloadCallback&render=explicit&hl=it' async defer></script>
</head>
<body>

<form method="post" action="contact.php" name="contactform" id="contactform">
<fieldset>
    <input name="email" type="text" id="email" required/>
    <input type="submit" class="submit" id="submit" value="Contact!" /> 
</fieldset>
</form>

<script type="text/javascript">

var onSubmit = function(token) {

    // ajax call

};

var onloadCallback = function() {
    grecaptcha.render('submit', {
        'sitekey' : 'PUBLIC_KEY',
        'callback' : onSubmit
    });
};
</script>

和contact.php文件:

class Captcha
{

private $key = 'PRIVATE_KEY'; 
public $goo;

public function verify()
{
    $postData = http_build_query(
        array(
            'secret' => $this->key,
            'response' => $this->goo,
            'remoteip' => $_SERVER['REMOTE_ADDR']
        )
    );

    $options = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postData
        )
    );

    $context  = stream_context_create($options);
    $response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);

    return json_decode($response);
}
}

$captcha = new Captcha();
$captcha->goo = $_POST['g-recaptcha-response'];
$response = $captcha->verify();

if (!$response->success) {
// not works
}
else {
// works
}

我不明白什么是不正确的。感谢帮助!

4

0 回答 0