php – Recaptcha未使用file_get_contents进行验证
发布时间:2020-12-13 13:12:03 所属栏目:PHP教程 来源:网络整理
导读:对于为什么这不起作用感到困惑.提交表单时,我收到错误消息,这意味着我的重新验证失败. 从我的形式: div class="g-recaptcha" data-sitekey="(site-key)"/div PHP: if(isset($_POST['g-recaptcha-response'])){ $captcha=$_POST['g-recaptcha-response']; }
|
对于为什么这不起作用感到困惑.提交表单时,我收到错误消息,这意味着我的重新验证失败.
从我的形式: <div class="g-recaptcha" data-sitekey="(site-key)"></div> PHP: if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
$secretKey = "(secret-key)";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) === true) {
echo '<h3>Thanks for your message!</h3>';
} else {
echo '<h3>Error</h3>';
}
reCaptcha文档专门指定必须通过POST发送对
https://www.google.com/recaptcha/api/siteverify的请求的参数.您可以使用CURL.
$ch = curl_init();
curl_setopt_array($ch,[
CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',CURLOPT_POST => true,CURLOPT_POSTFIELDS => [
'secret' => $secretKey,'response' => $captcha,'remoteip' => $_SERVER['REMOTE_ADDR']
],CURLOPT_RETURNTRANSFER => true
]);
$output = curl_exec($ch);
curl_close($ch);
$json = json_decode($output);
// check response...
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
