Php reCAPTCHA始终无法验证
我有以下用于Google recaptcha的功能:Php reCAPTCHA始终无法验证,php,recaptcha,Php,Recaptcha,我有以下用于Google recaptcha的功能: function isNotSpam(){ if(isset($_POST['g-recaptcha-response'])){ try { $url = 'https://www.google.com/recaptcha/api/siteverify'; $data = ['secret' => $google_captcha_secert,
function isNotSpam(){
if(isset($_POST['g-recaptcha-response'])){
try {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = ['secret' => $google_captcha_secert,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']];
$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;}
}else{return null;}
}
然后,我通过以下方式检查错误:
$google_captcha_secert = "/my secret key here/";
if(!isNotSpam()){display error here}
My HTML包含以下reCAPTCHA代码
<script src="https://www.google.com/recaptcha/api.js?hl=en"></script>
</head>
<body>
...
<form>
...
<div class="g-recaptcha" data-sitekey="/ my public key here /"></div>
...
</form>
</body>
我试着用它来做
if(isNotSpam() == null){display error here}
什么都没用,它总是告诉我我是一个机器人,但这显然是不正确的。您能告诉我错误在哪里吗?
$google\u captcha\u secert
未在函数中定义,因为您既没有使用从全局范围导入它,也没有将它作为参数提供给函数
<> P>可能存在更多的错误,考虑日志错误响应/异常,而不是默默吞咽它们,以便能够自行调试此类问题。我还建议在调试时启用错误报告。PHP应该在那里打印一个通知
if(isNotSpam() == null){display error here}