#### 1. 前言 --- [阿里云云市場(chǎng)實(shí)名認(rèn)證接口](https://market.aliyun.com/products/57000002/cmapi00037883.html?spm=5176.730005.result.14.49ec3524lCcMdf#sku=yuncode31883000010 "阿里云云市場(chǎng)實(shí)名認(rèn)證接口") 最近在做一個(gè)手機(jī) APP 項(xiàng)目,需要用到實(shí)名認(rèn)證接口,這是公司在阿里云云市場(chǎng)找的一個(gè)接口 #### 2. 代碼示例 --- 將下面函數(shù)中的 appcode 修改為云市場(chǎng)的真實(shí) appcode 即可使用 ```php /** * 阿里云實(shí)名認(rèn)證 API * @param $realName 姓名 * @param $cardNo 身份證號(hào) * @return boolean true|false 認(rèn)證成功|認(rèn)證失敗 */ function identityAuth($realName, $cardNo) { $host = "https://zid.market.alicloudapi.com"; $path = "/idcheck/Post"; $method = "POST"; $appcode = ''; if (empty($appcode)) { throw new \Exception("未配置 App Code"); } $headers = [ "Authorization:APPCODE {$appcode}", "Content-Type:application/x-www-form-urlencoded; charset=UTF-8", ]; $bodys = "cardNo={$cardNo}&realName={$realName}"; $url = $host . $path; $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); if (1 == strpos("$" . $host, "https://")) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys); $result = curl_exec($curl); if (empty($result)) { throw new \Exception("實(shí)名認(rèn)證接口異常"); } $array = json_decode($result, true); if ($array['error_code'] == 0 && $array['result']['isok'] === true) { return true; // 認(rèn)證成功 } else { return false; // 認(rèn)證失敗 } } ``` 調(diào)用示例 ```php // 調(diào)用 API 進(jìn)行實(shí)名認(rèn)證 if (!identityAuth($name, $idcard)) { throw new \Exception("認(rèn)證失敗,請(qǐng)檢查填寫(xiě)的信息"); } ``` #### 3. 返回值示例 --- 本文介紹的 API 返回值示例,也就是 `identityAuth()` 函數(shù)中的 `$result` 的值 特別注意: error_code 只表示 API 請(qǐng)求成功,實(shí)名認(rèn)證結(jié)果必須使用 `result.isok` 判斷 認(rèn)證成功時(shí)的返回值: 當(dāng)姓名和身份證號(hào)碼匹配時(shí), 認(rèn)證成功,isok 的值為 true ```json { "error_code":0, "reason":"成功", "result":{ "realname":"梁**", "idcard":"410225************", "isok":true, "IdCardInfor":{ "province":"河南省", "city":"開(kāi)封市", "district":"蘭考縣", "area":"河南省開(kāi)封市蘭考縣", "sex":"男", "birthday":"1999-10-16" } }, "sn":"0809093446637651130008663231" } ``` 認(rèn)證失敗時(shí)的返回值: 當(dāng)姓名和身份證號(hào)碼錯(cuò)誤或不匹配時(shí), 返回值如下所示,isok 的值為 false ```json { "error_code":0, "reason":"成功", "result":{ "realname":"張*", "idcard":"410225************", "isok":false, "IdCardInfor":null }, "sn":"080909320535865586360802173" } ```