阿里云智能验证

需要知道

accessKey,accessSecret

需要开通智能认证功能

<?php
include_once './aliyun-php-sdk-core/Config.php';
use  afs\Request\V20180112 as Afs;

$accessKey = !empty($_REQUEST['accessKey']) ? trim($_REQUEST['accessKey']) : '';
$accessSecret = !empty($_REQUEST['accessSecret']) ? trim($_REQUEST['accessSecret']) : '';

//YOUR ACCESS_KEY、YOUR ACCESS_SECRET请替换成您的阿里云accesskey id和secret
$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $accessKey, $accessSecret);
$client = new DefaultAcsClient($iClientProfile);
DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "afs", "afs.aliyuncs.com");

$request = new Afs\AuthenticateSigRequest();

$sessionId = !empty($_REQUEST['sessionId']) ? trim($_REQUEST['sessionId']) : '';
$token = !empty($_REQUEST['token']) ? trim($_REQUEST['token']) : '';
$sig = !empty($_REQUEST['sig']) ? trim($_REQUEST['sig']) : '';
$scene = !empty($_REQUEST['scene']) ? trim($_REQUEST['scene']) : '';
$ip = !empty($_REQUEST['ip']) ? trim($_REQUEST['ip']) : '';
$appKey = !empty($_REQUEST['appKey']) ? trim($_REQUEST['appKey']) : '';

$request->setSessionId($sessionId);// 会话ID。必填参数,从前端获取,不可更改。
$request->setToken($token);// 请求唯一表示。必填参数,从前端获取,不可更改。
$request->setSig($sig);// 签名串。必填参数,从前端获取,不可更改。
$request->setScene($scene);// 场景标识。必填参数,从前端获取,不可更改。
$request->setAppKey($appKey);// 应用类型标识。必填参数,后端填写。
$request->setRemoteIp($ip);// 客户端IP。必填参数,后端填写。
$response = $client->getAcsResponse($request);// 返回code 100表示验签通过,900表示验签失败
$res = get_object_vars($response);

if ((int)$res['Code'] === 100) {
    $return = array('status' => 0, 'info' => $res);
    ajaxReturn($return);
} else {
    $return = array('status' => 900, 'info' => $res);
    ajaxReturn($return);
}


function ajaxReturn($array){
    $content=json_encode($array);
    if(empty($_GET['callback'])){
        echo $content;exit;
    }else{
        echo $_GET['callback']. '(' .$content. ')';exit;
    }
}

返回100就是成功,返回900就是失败。

$urlAfs = C('SELF_HOST').'aliyunAfs/index.php';
$dataAfs['sessionId'] = $_POST['sessionId'];
$dataAfs['token'] = $_POST['token'];
$dataAfs['scene'] = $_POST['scene'];
$dataAfs['sig'] = $_POST['sig'];
$dataAfs['appKey'] = C('ALI_CAPTCHA_APP_KEY');
$dataAfs['ALI_ACCESS_KEY'] = C('ALI_ACCESS_KEY');
$dataAfs['ALI_ACCESS_SECRET'] = C('ALI_ACCESS_SECRET');
$dataAfs['ip'] = Func::getIp();

setlog($dataAfs,[],'dataAfs','afs.log');

$return_data = Http::newDoPost($urlAfs,$dataAfs,3);
$return_data = json_decode($return_data,true);
setlog($return_data,[],'return_data','afs.log');

if ((string)$return_data['status'] !== '0') {
    $this->json->E('人机验证失败',41571);
}

posted @ 2019-11-30 17:41  TBHacker  阅读(1295)  评论(0编辑  收藏  举报