PHP实现阿⾥云⾦融级实⼈认证⼈脸识别h5案例源码,带签名 PHP部分:
//本代码段为项⽬中的代码,部分参数接收⽅式和路径请⾃⾏更改
<?php
class control_face_certification
{
function __construct()
{
date_default_timezone_set("GMT");
$this->params = $_REQUEST;
}
public function index(){
include('view/face_certification/face_certification.html');
}
//公共参数部分
public function get_public_params()
{
return [
"Format" =>'json',
"Version" => "2019-03-07",
"AccessKeyId" => "------",
"AccessKeySecret" => "-----",
"SignatureMethod" => "HMAC-SHA1",
"Timestamp" =>date('Y-m-d\TH:i:s\Z'),
"SignatureVersion" => "1.0",
"SignatureNonce" =>  md5(rand(1000,9999)),
"SceneId" => -------
];
}
//获取认证链接
public function face_cert_url()
{
$data = $this->params;
$public_params = $this->get_public_params();
$url = "cloudauth.aliyuncs/?";
$Action = "InitFaceVerify";
$Format = $public_params["Format"];
$Version = $public_params["Version"];
$AccessKeyId = $public_params["AccessKeyId"];
$AccessKeySecret = $public_params["AccessKeySecret"];
$SignatureMethod = $public_params["SignatureMethod"];
$Timestamp = $public_params["Timestamp"];
$SignatureVersion = $public_params["SignatureVersion"];
$SignatureNonce = $public_params["SignatureNonce"];
$SceneId = $public_params["SceneId"];
$OuterOrderNo = md5(time().rand(1000,9999));
$ProductCode = "ID_PRO";
$CertType = "IDENTITY_CARD";
$CertName = $data["name"];
$CertNo = $data["idcard"];
$ReturnUrl = "".$_SERVER['HTTP_HOST']. /index.php?m=face_certification&a=call_back";
$MetaInfo = $data["metainfo"];
$MetaInfo = json_encode($MetaInfo,JSON_UNESCAPED_SLASHES);
$all_params = [
"Format" => $Format,
"Version" => $Version,
"AccessKeyId" => $AccessKeyId,
"SignatureMethod" => $SignatureMethod,
"Timestamp" => $Timestamp,
"SignatureVersion" => $SignatureVersion,
"SignatureNonce" => $SignatureNonce,
"Action" => $Action,
"SceneId" => $SceneId,
"OuterOrderNo" => $OuterOrderNo,
"ProductCode" => $ProductCode,
"CertType" => $CertType,
"CertName" => $CertName,
"CertNo" => $CertNo,
"ReturnUrl" => $ReturnUrl,
"MetaInfo" => $MetaInfo
];
$Signature = $this->make_sign($all_params,$AccessKeySecret);
$all_params["Signature"] = $Signature;
$res = $this->callInterface($url,$all_params);
return $res;
}
//认证完成后跳转接⼝
public function call_back()
{
$data = $this->params;
$response = stripslashes($data["response"]);
$result = json_decode($response,true);
$CertifyId = $result["extInfo"]["certifyId"];
$face_result = $this->get_face_result($CertifyId);
$face_result = json_decode($face_result,true);
$Code = $face_result["Code"];jquery源码在线
$Passed = $face_result["ResultObject"]["Passed"];
include('view/face_certification/face_result.html');
$filename = $_SERVER['DOCUMENT_ROOT'] . "/";
$log_time = "\n-----" . date("Y-m-d H:i:s") . "-----------------------\n";
file_put_contents($filename,$log_time."输⼊response    ".$response,FILE_APPEND);
$log_time = "\n-----" . date("Y-m-d H:i:s") . "-----------------------\n";
file_put_contents($filename,$log_time."输⼊face_result".json_encode($face_result),FILE_APPEND);    }
//确认识别结果
public function get_face_result($CertifyId)
{
$public_params = $this->get_public_params();
$url = "cloudauth.aliyuncs/?";
$Action = "DescribeFaceVerify";
$Format = $public_params["Format"];
$Version = $public_params["Version"];
$AccessKeyId = $public_params["AccessKeyId"];
$AccessKeySecret = $public_params["AccessKeySecret"];
$SignatureMethod = $public_params["SignatureMethod"];
$Timestamp = $public_params["Timestamp"];
$SignatureVersion = $public_params["SignatureVersion"];
$SignatureNonce = $public_params["SignatureNonce"];
$SceneId = $public_params["SceneId"];
$all_params = [
"Format" => $Format,
"Version" => $Version,
"AccessKeyId" => $AccessKeyId,
"SignatureMethod" => $SignatureMethod,
"Timestamp" => $Timestamp,
"SignatureVersion" => $SignatureVersion,
"SignatureNonce" => $SignatureNonce,
"SceneId" => $SceneId,
"CertifyId" => $CertifyId
];
$Signature = $this->make_sign($all_params,$AccessKeySecret);
$all_params["Signature"] = $Signature;
$res = $this->callInterface($url,$all_params);
return $res;
}
//签名
public function make_sign($all_params,$AccessKeySecret)
{
ksort($all_params);
$canonicalizedQueryString = '';
foreach($all_params as $key => $value)
{
$canonicalizedQueryString .= '&' . $this->percentEncode($key)
. '=' . $this->percentEncode($value);
}
$stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
$Signature = base64_encode(hash_hmac('sha1', $stringToSign, $AccessKeySecret . '&', true));
return $Signature;
}
//发送最终阿⾥接⼝请求
public function callInterface($url,$all_params){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . http_build_query($all_params));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
return $res;
}
//参数编码
public function percentEncode($str)
{
// 使⽤urlencode编码后,将"+","*","%7E"做替换即满⾜ECS API规定的编码规范
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
}
>
html部分:
face_certification.html
<html>
<div>真实姓名:<input type="text" id="name" />
⾝份证号:<input type="text" id="idcard" /></div>
<br />
<button id="go_face_cert">去实名认证</button>
</html>
<script src="<?php echo __PUBLIC__?>web/weUI/js/jquery-1.12.1.min.js" type="text/javascript"></script>
<script type="text/javascript"
src="cn-shanghai-aliyun-cloudauth.oss-cn-shanghai.aliyuncs/web_sdk_js/jsvm_all.js"></script>
$(function(){
var metainfo = getMetaInfo();
$("#go_face_cert").click(function(){
var name = $("#name").val();
var idcard = $("#idcard").val();
if(name == "")
{
alert("姓名不能为空!");
return;
}
if(idcard == "")
{
alert("⾝份证号不能为空!");
return;
}
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if(!st(idcard))
{
alert("⾝份证号格式错误!");
return;
}
$.ajax({
type: "POST",
url: "index.php?m=face_certification&a=face_cert_url",        data: {
"metainfo":metainfo,
"name":name,
"idcard":idcard
},
dataType: "json",
success: function (res) {
console.log(res);
if(res.Code == 200)
{
var face_cert_url = res.ResultObject.CertifyUrl;                window.location.href = face_cert_url;
}
else
{
alert("服务器错误"+res.Message);
}
},
error: function () {
alert("服务器错误");
}
});
})
});
</script>
face_result.html
<html>
<?php
if($Code == 200)
{
if($Passed == "T")
{
echo "认证通过";        }
else
{
echo "认证不通过";        }
}
else
{
echo "认证失败";
}
>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。