Alibaba Cloud Official Website Reference Document: Bank Card Element Verification - Enhanced Real Person Authentication - Alibaba Cloud Documentation Center
Preparation:
One: Log in to the Alibaba Cloud console and select real-person authentication
The function in the upper left corner is switched to the enhanced version of real person authentication
Access scene settings--"Card verification--"Authentication scene ID [needed in the code]
Two: Click AccessKey Management in the avatar drop-down box
View AccessKey ID and Secret [needed in the code]
Two: Execute the command to download the php SDK
composer require alibabacloud/cloudauth-20200618 2.0.4
Note: If the composer command is executed due to lack of composer.lock file or other reasons in the project, the library files introduced by the original composer in the project will be updated.
The solution is to create a new folder on the local computer, use the command window to execute the above command to download the SDK, then create a new folder in the project, the name can be named sdk, and upload the downloaded SDK resource package to the Folder, just import the resource bundle into the project.
Code reference path: Alibaba Cloud OpenAPI Developer Portal https://next.api.aliyun.com/api/Cloudauth/2020-06-18/VerifyBankElement?params={}&lang=PHP&tab=DEMO
Note that the correct version should be selected, which should be consistent with the version of the resource package downloaded by composer above.
The following is the code I compiled. The sdk download method uses the above method to download to the local and then put it into the project.
//验证姓名、身份证号、银行卡号、手机号是否匹配
function checkBankNoByAli($data){
$rs=array('code'=>0,'msg'=>'','info'=>array());
$ali_realauth_sceneid=''; //认证场景ID
$ali_accesskeyid=''; //accesskeyid
$ali_accesskeysecret=''; //secret
if(!$ali_realauth_sceneid){
$rs['code']=1001;
$rs['msg']='认证场景ID错误';
return $rs;
}
if(!$ali_accesskeyid){
$rs['code']=1001;
$rs['msg']='accesskeyid错误';
return $rs;
}
if(!$ali_accesskeyid){
$rs['code']=1001;
$rs['msg']='accesskeysecret错误';
return $rs;
}
require_once '/sdk/alicloudauth/autoload.php'; //路径需替换为自己项目中的实际路径
$client = createClient($ali_accesskeyid, $ali_accesskeysecret);
$result=json_decode($result,true);
$verifyBankElementRequest = new AlibabaCloud\SDK\Cloudauth\V20200618\Models\VerifyBankElementRequest([
"sceneId" => $ali_realauth_sceneid,
"outerOrderNo" => random(32),
"mode" => "VERIFY_BANK_CARD",
"bankCardNo" => $data['bankno'],
"idNo" => $data['cardno'],
"idName" => $data['realname'],
"mobile" => $data['mobile']
]);
$runtime = new AlibabaCloud\Tea\Utils\Utils\RuntimeOptions([]);
try {
$result = $client->verifyBankElementWithOptions($verifyBankElementRequest, $runtime);
$result=json_encode($result);
$result=json_decode($result,true);
$code=$result['body']['code'];
//$message=$result['body']['message'];
if($code !=200){
$rs['code']=1003;
$rs['msg']='验证失败';
return $rs;
}
$resultObject=$result['body']['resultObject'];
if(isset($resultObject['passed'])){
$return_status=$resultObject['passed'];
if($return_status !='T'){
$rs['code']=1004;
$rs['msg']='验证失败';
return $rs;
}
}
$return_code=$resultObject['subCode'];
if($return_code !=200){
$rs['code']=1005;
$rs['msg']='验证失败';
return $rs;
}
}
catch (Exception $error) {
if (!($error instanceof AlibabaCloud\Tea\Exception\TeaError)) {
$error = new AlibabaCloud\Tea\Exception\TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// 如有需要,请打印 error
// Utils::assertAsString($error->message);
$rs['code']=1006;
$rs['msg']=$error->message;
return $rs;
}
}
//阿里云证件号配置生成
function createClient($accessKeyId, $accessKeySecret){
$config = new Darabonba\OpenApi\Models\Config([
// 您的 AccessKey ID
"accessKeyId" => $accessKeyId,
// 您的 AccessKey Secret
"accessKeySecret" => $accessKeySecret
]);
// 访问的域名
$config->endpoint = "cloudauth.aliyuncs.com";
return new AlibabaCloud\SDK\Cloudauth\V20200618\Cloudauth($config);
}
//生成随机数
function random($length = 6 , $numeric = 0) {
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
if($numeric) {
$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}
//调用方法
public function test(){
$check_data=array(
'bankno' =>'', //银行卡号
'realname' =>'', //姓名
'cardno' =>'', //身份证号
'mobile' =>'' //手机号
);
$result=checkBankNoByAli($check_data);
if($result['code']!=0){
return $result;
}else{
return array(
'code'=>0,
'msg'=>'验证成功',
'info'=>array()
);
}
}
One thing to note: If you repeatedly query the same information more than 10 times within 24 hours, you will be locked for 12 hours. Control the number of requests during the test.