基于支付宝芝麻信用人脸识别的实名认证服务 · 为其他网站提供 API 调用
本系统对接支付宝芝麻认证(人脸识别),为第三方网站提供统一的实名认证 API 服务。第三方网站通过分配的 app_key 和 app_secret 调用接口,实现姓名 + 身份证号 + 人脸识别的实名认证。
POST /api/init.php 发起认证 → 获得 certify_urlcertify_url 完成人脸识别POST /api/query.php 查询认证结果(或等待异步通知)
所有 API 请求需在 HTTP Header 中携带以下参数:
| Header | 说明 |
|---|---|
| X-App-Key | 应用 Key(管理后台分配) |
| X-Timestamp | 当前时间戳(秒),5分钟内有效 |
| X-Sign | 签名,算法见下方 |
签名算法:
sign = md5( app_key + timestamp + body字符串 + app_secret )
示例:
app_key = zm_xxxxxxxxxxxxxxxx
timestamp = 1697000000
body = {"out_order_no":"ORDER001","name":"张三","id_card":"110101199001011234"}
app_secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
待签名字符串 = "zm_xxxxxxxxxxxxxxxx1697000000{\"out_order_no\":\"ORDER001\",\"name\":\"张三\",\"id_card\":\"110101199001011234\"}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
sign = md5(待签名字符串)
请求参数(JSON Body):
| 参数 | 必填 | 说明 |
|---|---|---|
| out_order_no | 是 | 商户订单号,调用方唯一 |
| name | 是 | 真实姓名 |
| id_card | 是 | 身份证号 |
| phone | 否 | 手机号 |
| return_url | 否 | 认证完成回跳地址,不传用应用配置 |
响应示例:
{
"code": 0,
"msg": "success",
"data": {
"out_order_no": "ORDER001",
"biz_no": "ZM202310101234567890",
"certify_url": "https://zmopenapi.zmxy.com.cn/..."
}
}
certify_url 后,将用户重定向到该 URL 完成人脸识别。请求参数(JSON Body):
| 参数 | 必填 | 说明 |
|---|---|---|
| out_order_no | 二选一 | 商户订单号 |
| biz_no | 二选一 | 芝麻业务号 |
响应示例:
{
"code": 0,
"msg": "success",
"data": {
"out_order_no": "ORDER001",
"biz_no": "ZM202310101234567890",
"name": "张三",
"id_card": "1101**********1234",
"status": "success",
"passed": true,
"fail_reason": "",
"certified_at": "2023-10-10 12:34:56"
}
}
status 取值:pending 待认证 / success 成功 / fail 失败
function apiSign($appKey, $timestamp, $body, $appSecret){
return md5($appKey . $timestamp . $body . $appSecret);
}
$appKey = 'zm_xxxxxxxxxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$timestamp = time();
$body = json_encode([
'out_order_no' => 'ORDER' . time(),
'name' => '张三',
'id_card' => '110101199001011234',
], JSON_UNESCAPED_UNICODE);
$sign = apiSign($appKey, $timestamp, $body, $appSecret);
$ch = curl_init('https://your-domain.com/api/init.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-App-Key: ' . $appKey,
'X-Timestamp: ' . $timestamp,
'X-Sign: ' . $sign,
]);
$resp = curl_exec($ch);
curl_close($ch);
$result = json_decode($resp, true);
if ($result['code'] === 0) {
// 重定向用户到刷脸页面
header('Location: ' . $result['data']['certify_url']);
}
支付宝在用户完成认证后,会异步通知 /api/notify.php,系统自动验签并更新认证状态。如需通知到接入方业务系统,可在应用配置中填写 callback_url。