OverviewAll the examples in this document are based on PHP framework.
Development environmentItem
Description
Compatibility
php5.2+,php7
Dependency
curl
ResourcesItem
Description
Data communication flow chart
Download SDK
PHP SDK Installation
There are two ways to download SDK.
Download SDKUse command line to get from Github
git clone https://github.com/GeeTeam/gt3-php-sdk.git
ownload SDK manually
Get the latest SDK from Github: gt3-php-sdk (in .zip format).
Import SDK
Import the SDK to your project with the following code example.
require_once dirname(dirname(__FILE__)) . '/lib/class.geetestlib.php';
Configure key pair and modify the requestConfigure key pair
Get your key pair from GeeTest Dashboard. The key pair consists of a public key(captcha ID) and a private key (KEY). Then, configure the key pair in the following path.
/config/config.php
Modify the request (optional)
Variable
Description
user_id
User identification. If you concern about the risk of user information, you can preprocess (e.g. hashed) it.
client_type
Type of client. Web (web browser for computers),h5 (mobile browser,include webview),native(native mobile app),unknown (unknow client type)
ip_address
Client request for your server IP address, unknow means unknow IP address
API example
Captcha initialization
Initiate captcha via API1, get challenge and set the status.
require_once dirname(dirname(__FILE__)) . '/lib/class.geetestlib.php';
require_once dirname(dirname(__FILE__)) . '/config/config.php';
$GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
session_start();
$data = array(
"user_id" => "test", # 网站用户id
"client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
"ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP
);
$status = $GtSdk->pre_process($data, 1);
$_SESSION['gtserver'] = $status;
$_SESSION['user_id'] = $data['user_id'];
echo $GtSdk->get_response_str();
Notice:status indicates captcha initialization. Status=1 refers to successful initialization, status=0 refers to downtime. Please store the status, since it will be needed in secondary verification. In the demo above, session has been used to store status.
Secondary verification (API2), including uptime and downtimerequire_once dirname(dirname(__FILE__)) . '/lib/class.geetestlib.php';
require_once dirname(dirname(__FILE__)) . '/config/config.php';
session_start();
$GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
$data = array(
"user_id" => $_SESSION['user_id'], # 网站用户id
"client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
"ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP
);
if ($_SESSION['gtserver'] == 1) { //服务器正常
$result = $GtSdk->success_validate($_POST['geetest_challenge'], $_POST['geetest_validate'], $_POST['geetest_seccode'], $data);
if ($result) {
echo '{"status":"success"}';
} else {
echo '{"status":"fail"}';
}
} else { //服务器宕机,走failback模式
if ($GtSdk->fail_validate($_POST['geetest_challenge'],$_POST['geetest_validate'],$_POST['geetest_seccode'])) {
echo '{"status":"success"}';
} else {
echo '{"status":"fail"}';
}
}
How to simulate the Failback mode? Please fill in an incorrect string (e.g. 123456789) for the captcha ID. Then, it will enter the Failback mode.
Run demo
Upload the demo to the root folder and run the project.
To view the demo, please visit http://127.0.0.1/gt3-php-sdk/static/login.html in browser.
Troubleshooting: secondary verification failureSDK internal logic errors. Please check: a) whether session is stored and read successfully, b) whether the code could successfully process to the module which send a request to GeeTest server, c) check the return value of GeeTest server.
Multiple submission. The API which initiates the secondary verification is only available for one time.
The challenge is inconsistent. Please ensure the consistency of challenge during the whole verification process.
Check if gt and key in the code are correct in SDK. Please check if the parameters has been passed correctly.
Please provide challenge to our service team. They could help you to check the log.