您现在的位置是:首页  > 前端  > 后端  > WEB基础 WEB基础

谷歌验证码reCAPTCHA接口在网页中的使用

2020-08-20【WEB基础】2626人已围观

简介### reCAPTCHA v2 > https://www.google.com/recaptcha/admin/site/431807236/setup ``` cs.apidata.vip // 在您的网站提供给用户的 HTML 代码中使用此网站密钥 6LcE27wZAAAAAF4ggKUdK7OiKfTxhVsSWDZWqQYa // 此密钥用于您的网站和 reCAPTCHA 之间的通信 6LcE27wZAAAAAC3ECu5DsoXMcnu5nV53juIhSrPV ``

reCAPTCHA v2

https://www.google.com/recaptcha/admin/site/431807236/setup

  1. cs.apidata.vip
  2. // 在您的网站提供给用户的 HTML 代码中使用此网站密钥
  3. 6LcE27wZAAAAAF4ggKUdK7OiKfTxhVsSWDZWqQYa
  4. // 此密钥用于您的网站和 reCAPTCHA 之间的通信
  5. 6LcE27wZAAAAAC3ECu5DsoXMcnu5nV53juIhSrPV

HTML

  1. <!-- 第一步引入 js -->
  2. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  3. <!-- 加入代码 填写html密钥 -->
  4. <div class="g-recaptcha" data-sitekey="6LcE27wZAAAAAF4ggKUdK7OiKfTxhVsSWDZWqQYa"></div>

PHP

  1. <?php
  2. $secretKey = "6LcE27wZAAAAAC3ECu5DsoXMcnu5nV53juIhSrPV";
  3. $code = $_REQUEST['g-recaptcha-response'];
  4. if($code == ""){
  5. _json(0,"Please click Google to verify");
  6. }
  7. $code_re = http_request("https://www.google.com/recaptcha/api/siteverify",["secret"=>$secretKey,"response"=>$code]);
  8. if($code['success'] === false){
  9. _json(0,"Google verify ".json_encode($code_re['error-codes']));
  10. }

reCAPTCHA v3

https://www.google.com/recaptcha/admin/site/432089010/setup

  1. 6LeyJ8EZAAAAAOBIjx7Ql6cqC_VllMVJpYRLNFth
  2. 6LeyJ8EZAAAAADL2hn-NOD7NxMJcSfN4irzDG025

HTML

  1. <button class="g-recaptcha"
  2. data-sitekey="reCAPTCHA_site_key"
  3. data-callback='onSubmit'
  4. data-action='submit'>Submit</button>

PHP 同v2

文档地址 https://developers.google.com/recaptcha/docs/v3

使用到的函数

  1. /**
  2. * 输出json
  3. * @param int $status
  4. * @param string $msg
  5. * @param array $data
  6. */
  7. function _json($status=1, $msg="ok", $data=[], $option=[])
  8. {
  9. header("Content-type:application/json;charset=utf-8");
  10. $re = ["code"=>(int)$status,"msg"=>(string)$msg,"data"=>$data];
  11. $re = array_merge($re, $option);
  12. echo json_encode($re,256);
  13. exit;
  14. }
  15. /**
  16. * http请求
  17. * @param $url
  18. * @param string $data
  19. * @param bool $pem
  20. * @return mixed|string
  21. */
  22. function http_request($url,$data='',$pem=false){
  23. $ch= curl_init();
  24. curl_setopt($ch, CURLOPT_URL, $url );
  25. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  26. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  27. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置内容是不是返回
  28. if(!empty($data)){
  29. curl_setopt($ch, CURLOPT_POST, 1); //设置post提交数据
  30. curl_setopt($ch,CURLOPT_POSTFIELDS,$data); //设置post提交数据
  31. }
  32. //携带证书
  33. if(!empty($pem)){
  34. curl_setopt($ch,CURLOPT_SSLCERT,CERT_PATH); //设置post提交数据
  35. curl_setopt($ch,CURLOPT_SSLKEY,KEY_PATH);
  36. curl_setopt($ch,CURLOPT_CAINFO,KEY_PATH);
  37. }
  38. //判断当前是不是有post数据的发
  39. $output=curl_exec($ch);
  40. if ($output === FALSE) {
  41. $output="curl 错误信息: " . curl_error($ch);
  42. }
  43. curl_close($ch);
  44. return $output;
  45. }


关注博客,更多精彩分享,敬请期待!
 

Tags:

很赞哦! (0)

我的名片

网名:随心

职业:PHP程序员

现居:湖北省-武汉市

Email:704061912@qq.com