Think PHP 下使用极验验证

首先我们先打开极验官网:www.geetest.com注册账号,并申请一个API key 它回会给以下两个值CAPTCHA_ID和PRIVATE_KEY,再在极验下载他给你的SDK或者用git下载 ,,下载命令为:git clone https://github.com/GeeTeam/gt3-php-sdk.git。

Think PHP 下调用极验SDK

我们先在 application文件夹里面新建一个common文件夹,再在common文件夹里面新建一个geetest文件夹来存下载下来的SDK核心文件lib文件夹下的Geetestlib.php以及web文件夹下的所有文件 ,并加上命名空间 例:namespace app\common\geetest\lib\GeetestLib

目录样式为:

  • Geetest
    • config
      • config.php
    • lib
      • Geetestlib.php
    • web
      • StartCaptchaServlet.php
      • VerifyLoginServlet.php

添加到application之后目录样式为:

  • application
    • common
      • geetest
        • config
          • config.php
        • Lib
          • Geetestlib.php
        • web
          • StartCaptchaServlet.php
          • VerifyLoginServlet.php

在common文件下新建一个Geetest.php文件并写下如下代码

<?php
       namespace app\model

       use think\session;
       use app\common\geetest\Lib;

       define("CAPTCHA_ID","2472e2b617c9fc69493419624a713036");
		 
       define("PRIVATE_KEY","33be4d2a044e17ed1345f6bde2aefe49");

       class Geetest
	{

		public $param = [
			'user_id' => 'test',
			'client_type' => 'web',
			'ip_address' => '127.0.0.1'
		];
		
		public function StartCaptchaServlet()
		{
			# code...

			$Geetest = new GeetestLib(CAPTCHA_ID,PRIVATE_KEY);
			$status = $Geetest -> pre_process($this -> param,1);
			Session::set('gtserver',$status);
			Session::set('user_id',1);
			return $Geetest -> get_response_str();
		}


		public function Validate($param)
		{
			# code...
			$Geetest = new GeetestLib(CAPTCHA_ID,PRIVATE_KEY);
			if(Session::get('gtserver') == 1){
				$result = $Geetest -> success_validate($param['geetest_challenge'],$param['geetest_validate'],$param['geetest_seccode'],$this -> param);
				if($result){
					return true;
				}else{
					return false;
				}
			}else{
				$result = $Geetest -> fail_validate($param['geetest_challenge'],$param['geetest_validate'],$param['geetest_seccode']);
				if($result){
					return true;
				}else{
					return false;
				}
			}

		}
?>

在index/controller/index.php文件下建立模块

    namespace app\index\controller;

    use think\Controller;

    class Index extends Controller{
		public function Geetest(){
			$request = request::instance();
			$data = input('post.');
			$model = $request -> get('model');
			if($model == 1){ //第一次请求验证
				$geetest = new Geetest();
				if ($r = $geetest -> StartCaptchaServlet()) {
					# code...
					return $r;
				} else {
					# code...
					return false;
				}
				
				return $model;
			}else if($model == 2){ //第二次核实请求
				if ($r = $geetest -> Validate($data)) {
					# code...
					return '{"tip":200,"message":"验证成功,三秒后自动跳转~"}';
				} else {
					# code...
					return '{"tip":500,"message":"验证失败"}';
				}
				
			}else{
				header("HTTP/1.0 404 Not Found");
			}
		}
    }

前端界面(乱做一下)记得一定要引入gt.js文件(下载的SDK里面有)

<script src="./assets/js/jquery-3.3.1.min.js"></script>
<script src="./assets/js/gt.js"></script>
<p class="tip-show"></p>
<div class="btn" onclick="geetest"><span>点击验证</span></div>
<div id="popup-captcha"></div>
<script>
	function geetest(){
		var handlerPopup = function(capchaObj){
			capchaObj.onSuccess(function(){
				var validate = capchaObj.getValidate();
				$.ajax({
					url:'http://test.com/index.php/index/geetest?model=1',
					type:'post',
					data:{
						type:'pc',
						geetest_challenge:validate.geetest_challenge,
						geetest_validate:validate.geetest_validate,
						geetest_seccode:validate.geetest_seccode
					},
					success:function(data){
						if(data.tip == 200){
			                   $('.tip-show').html(data.message);
		                 }else{
			                  $('.tip-show').html(data.message);
			                  $('#popup-captcha').hide();
			                  $('.btn').show();
		                }
                    }
        );},
					error:function(){
						$('.tip-show').html(data.message);
					}
				});
			});

			$('#popup-submit').click(function(){
				capchaObj.show();
			});

			capchaObj.appendTo('#popup-captcha');
			$('tip-show').html('');


		}

		$.ajax({
			url:'http://test.com/index.php/index/geetest?model=1',
			type:'get',
			dataType:'json',
			success:function(){
				$('.btn').hide();
                $('#popup-captcha').html('');
				$('#popup-captcha').show();
				initGeetest({
					gt:data.gt,
					challenge:data.challenge,
					product:'embed',
					offline:!data.success
				},handlerPopup);
			},
			error:function(){
				$('#popup-captcha').hide();
				$('.btn').show();
				$('tip-show').html('');
			}
		});
</script>

点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注