来自我的博客mcoo.me
- 注册账号
注册地址:http://www.geetest.com/
注册之后获得最新的ID和KEY
2·下载sdk
把下载下来的sdk改名为geetest放入vendor文件下
可以把geetest文件下的其他文件都删除只保留lib文件
3·把lib下的class.geetestlib.php修改名称为:GeetestLib.class.ph 修改GeetestLib类里的pre_process()方法
public function pre_process($param, $new_captcha=1) {
$data = array('gt'=>$this->captcha_id,
'new_captcha'=>$new_captcha
);
$data = array_merge($data,$param);//没有修改的这里可能会报错
$query = http_build_query($data);
$url = "http://api.geetest.com/register.php?" . $query;
$challenge = $this->send_request($url);
if (strlen($challenge) != 32) {
$this->failback_process();
return 0;
}
$this->success_process($challenge);
return 1;
}
改成
public function pre_process($param, $new_captcha=1) {
$data = array('gt'=>$this->captcha_id,
'new_captcha'=>$new_captcha
);
if (($param != null) and (is_string($param))) {
$data['user_id'] = $param;
}
$query = http_build_query($data);
$url = "http://api.geetest.com/register.php?" . $query;
$challenge = $this->send_request($url);
if (strlen($challenge) != 32) {
$this->failback_process();
return 0;
}
$this->success_process($challenge);
return 1;
}
4·在composer.json的autoload内的classmap项新增类包,
"autoload": {
"classmap": [
"database",
"vendor/geetest"//添加自己的
],
"psr-4": {
"App\\": "app/"
}
},
5·运行终端,cd到项目路径,使用composer命令
composer dumpautoload
然后就能在项目中愉快的使用GeetestLib类了,简单的用法如下:
use GeetestLib;
$GtSdk = new GeetestLib()
6·我是在config文件下新建了sys.php做配置文件方便以后在后台修改
/*验证码配置*/
'GEE_ID' => '4c65ff2cf2d4ac493e837a91215fff77',
'GEE_KEY' => 'e9fc13ecf1178f6a59c80f8f42e5773f',
7·在App\Http\Controllers 下创建Geetest控制器方法getVerify()
namespace App\Http\Controllers;
use GeetestLib;
class GeetestController extends Controller
{
public function getVerify(){
//实例化并传入极验id与key值
$GtSdk = new GeetestLib(config('sys.GEE_ID'), config('sys.GEE_KEY'));
$user_id = "web";
$status = $GtSdk->pre_process($user_id);
$data = array(
'gtserver'=>$status,
'user_id'=>$user_id
);
session(['geetest'=>$data]);
echo $GtSdk->get_response_str();
}
}
创建路由
Route::get('/getVerify', 'GeetestController@getVerify')->name('getVerify');
8·在页面上处理
@extends('layouts.app')
@section('content')
Login
@endsection
9·初始化完成之后的提交时前段会自动验证,然后提交给后台之后处理方法是:
public function login()
{
$GtSdk = GeetestLib(config('sys.GEE_ID'), config('sys.GEE_KEY'));
$geetest = session("geetest");
$user_id = $geetest['user_id'];);
if ($geetest['gtserver'] == 1) {
$result = $GtSdk->success_validate($geetest_challenge, $geetest_validate, $geetest_seccode, $user_id);
if ($result) {
echo 'Yes!';
} else{
echo 'No';
}
}else{
if ($GtSdk->fail_validate($geetest_challenge, $geetest_validate, $geetest_seccode)) {
echo "yes";
}else{
echo "no";
}
}
}
然后在页面上就可以看到效果了。