使用PHP进行Google Invisible reCAPTCHA,无响应

时间:2018-04-14 21:19:27

标签: php recaptcha invisible

似乎无法让Google无形的reCAPTCHA工作。这应该很简单,希望找出我做错了什么。这是我的客户端代码:

<head>
<script type="text/javascript">

    var onSubmit = function(token) {
      console.log('success!');
    };

    var onloadCallback = function() {
      grecaptcha.render('submit', {
        'sitekey' : '------my site key---------',
        'callback' : onSubmit
      });
    };
</script>
</head>

形式:

<form action='/mail-script.php' target='_self' method='POST' id='econtact'>
<input class='w3-input' type='text' placeholder='Name' required name='Name'>
<input class='w3-input w3-section' type='text' placeholder='Email' required name='Email'>
<input class='w3-input w3-section' type='text' placeholder='Subject' required name='Subject'>
<input class='w3-input w3-section' type='text' placeholder='Comment' required name='Comment'>
<button class='w3-button w3-dark-blue w3-section' type='submit' name='submit'>
<i class='fa fa-paper-plane'></i> SEND MESSAGE</button></form>
<script src='https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit' async defer></script>

服务旁边代码:

<?php 
function checkCaptcha(){    
if(isset($_POST['g-recaptcha-response'])){
    $captcha = $_POST['g-recaptcha-response'];

    $postdata = http_build_query(
        array(
            'secret'   => '----------secret code----------',
            'response' => $captcha,
            '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);
    $result  = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context));

    return $result->success;
}else{
    return false;
    }
}
  $captcha_result = checkCaptcha();

echo("the captcha result is: " . $captcha_result);
?>

所以$ captcha_result是空白的。我无法检测到任何东西。我明白了 g-recaptcha-response没有正确地通过表单传递,或者正在进行其他操作。谢谢你的帮助。

更新: var_dump($ result); = NULL

0 个答案:

没有答案