本帖最后由 LeoWang2021 于 2021-8-3 14:08 编辑
使用PHP制作recaptcha验证时,提交验证码api出现了报错,希望有做过类似项目的指导一下,谢谢
代码如下:
文件名:index.html
[HTML] 纯文本查看 复制代码 <!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src='https://recaptcha.net/recaptcha/api.js'></script>
</head>
<body>
<form action="confirm.php" method="post">
<div class="g-recaptcha" data-sitekey="6Lep-8wbAAAAAHIYxFzMPcdaZteYV3i39R8UcwLa"></div>
<p><button class="btn btn-primary" type="submit">Register</button>
</form>
</body>
</html>
文件名:confirm.php
[PHP] 纯文本查看 复制代码 <span style="font-size:14px;"><!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php
function send_post($url, $post_data)
{
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
$post_data = array(
'secret' => '6Lep-8wbAAAAAMst122U4mSLT2qGGfF5Cw3K****',
'response' => $_POST["g-recaptcha-response"]);
$recaptcha_json_result = send_post('http://recaptcha.net/recaptcha/api/siteverify', $post_data);
$recaptcha_result = json_decode($recaptcha_json_result);
//在这里处理返回的值
var_dump($recaptcha_result);
?>
</head>
<body>
<?php
if($recaptcha_result['success'])
{
echo '校验成功';
}
else
{
echo '校验失败';
}
?>
</body>
</html>
</span>
或下载压缩包https://wwa.lanzoui.com/iGIZPs5fscd |