接入網易易盾驗證碼流程圖:
後端接入:http://support.dun.163.com/documents/15588062143475712?docId=69218161355051008
前端web接入:http://support.dun.163.com/documents/15588062143475712?docId=150442915877015552
官方PHP示例代碼:https://github.com/yidun/captcha-php-demo
這裏是基於 laravel 框架來實現
效果圖如下:
封裝service代碼如下:
<?php
namespace App\Services;
/**
* 易盾驗證碼二次校驗SDK
* WXiangQian <[email protected]>
*/
class CaptchaVerifier {
const VERSION = 'v2';
const API_TIMEOUT = 5;
const API_URL = 'http://c.dun.163yun.com/api/v2/verify';
protected $captcha_id;
protected $secret_id;
protected $secret_key;
/**
* 驗證碼id
* 密鑰對
* 密鑰對
* @param $captcha_id
* @param $secret_id
* @param $secret_key
*/
public function __construct($captcha_id, $secret_id, $secret_key) {
$this->captcha_id = $captcha_id;
$this->secret_id = $secret_id;
$this->secret_key = $secret_key;
}
/**
* 發起二次校驗請求
* 二次校驗數據
* @param $validate
* @param string $user 用戶信息
* @return bool|mixed
* User: WXiangQian <[email protected]>
* Date: 2019-05-13 17:22
*/
public function verify($validate, $user = '') {
$params = array();
$params["captchaId"] = $this->captcha_id;
$params["validate"] = $validate;
$params["user"] = $user;
// 公共參數
$params["secretId"] = $this->secret_id;
$params["version"] = self::VERSION;
$params["timestamp"] = sprintf("%d", round(microtime(true)*1000));// time in milliseconds
$params["nonce"] = sprintf("%d", rand()); // random int
$params["signature"] = $this->sign($this->secret_key, $params);
$result = $this->send_http_request($params);
return array_key_exists('result', $result) ? $result['result'] : false;
}
/**
* 計算參數簽名
* 密鑰對key
* 請求參數
* @param $secret_key
* @param $params
* @return string
* User: WXiangQian <[email protected]>
* Date: 2019-05-13 17:23
*/
private function sign($secret_key, $params){
ksort($params); // 參數排序
$buff="";
foreach($params as $key=>$value){
$buff .=$key;
$buff .=$value;
}
$buff .= $secret_key;
return md5($buff);
}
/**
* 發送http請求
* @param $params
* @return array|mixed
* User: WXiangQian <[email protected]>
* Date: 2019-05-13 17:23
*/
private function send_http_request($params){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::API_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::API_TIMEOUT);
curl_setopt($ch, CURLOPT_TIMEOUT, self::API_TIMEOUT);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
/*
* Returns TRUE on success or FALSE on failure.
* However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
*/
$result = curl_exec($ch);
// var_dump($result);
if(curl_errno($ch)){
$msg = curl_error($ch);
curl_close($ch);
return array("error"=>500, "msg"=>$msg, "result"=>false);
}else{
curl_close($ch);
return json_decode($result, true);
}
}
}
新建config/captcha.php 添加如下代碼
<?php
#聯繫商務獲取祕鑰
return [
'captcha'=>[
'CAPTCHA_ID' => env('CAPTCHA_ID'),
'SECRET_ID' => env('SECRET_ID'),
'SECRET_KEY' => env('SECRET_KEY'),
]
];
.env添加
#captcha
CAPTCHA_ID=CAPTCHA_ID
SECRET_ID=SECRET_ID
SECRET_KEY=SECRET_KEY
二次校驗
public function wangyiVerify(Request $request)
{
$validate = $request->input('validate');
// 文檔地址:http://support.dun.163.com/documents/15588062143475712?docId=69218161355051008
$CaptchaVerifier = new CaptchaVerifier(config('captcha.captcha.CAPTCHA_ID'),config('captcha.captcha.SECRET_ID'),config('captcha.captcha.SECRET_KEY'));
//通過則返回true
$validatePass = $CaptchaVerifier->verify($validate);
if (!$validatePass) {
return $this->responseError('驗證不通過');
}
return $this->responseSuccess('驗證通過');
}
項目中有示例代碼:https://github.com/WXiangQian/laravel-api
感謝大家的支持,希望可以幫助到大家