利用帝國cms在做一些會員系統的時候,需要做人臉識別認證,之前接入了某api接口,發現身份證識別率真的低,還好充值的少,否則要出問題,后來發現會員注冊率降低了不少,最終還是決定使用騰訊云的人臉識別,雖然費用高一點點,但是人臉識別率高,數據反饋很快。
其中的index.php是執行文件,ecms.php是操作文件,notify.php是數據毀掉文件,代碼其實很少,在微信中訪問直接跳轉到騰訊云的人臉識別,然后自動返回把相應的數據寫進數據庫,我這里很簡單,先進行認證后對數據庫的身份進行對比,是否認證過,還會清理之前點擊認證最后沒有認證完的數據,最大限度的情理無用的信息。
if($enews=='gofaceid') {//判斷該用戶是否認證過$del=$empire->query("delete from {$dbtbpre}member_verify where userid='$user[userid]';");//先刪除以前的class Face {const SecretId = "apiid";const SecretKey = "apikey";const Url = "https://faceid.tencentcloudapi.com";//算法const Algo = "sha256";//規范請求串const HTTPRequestMethod = "POST";const CanonicalURI = "/";const CanonicalQueryString = "";const CanonicalHeaders = "content-type:application/json; charset=utf-8\nhost:faceid.tencentcloudapi.com\n";const SignedHeaders = "content-type;host";//參與簽名的頭部信息//簽名字符串const Algorithm = "TC3-HMAC-SHA256";const Service = "faceid";const Stop = "tc3_request";/*** 實名核身鑒權*/public function getDetectAuth() {$param = ['RuleId' => "1",//用于細分客戶使用場景,申請開通服務后,可以在騰訊云慧眼人臉核身控制臺(https://console.cloud.tencent.com/faceid) 自助接入里面創建,審核通過后即可調用'RedirectUrl' => "跳轉地址",//用于細分客戶使用場景,申請開通服務后,可以在騰訊云慧眼人臉核身控制臺(https://console.cloud.tencent.com/faceid) 自助接入里面創建,審核通過后即可調用];return self::getCommonPostRequest("DetectAuth", $param);}/*** 鑒權* @param string $action 方法* @param array $param 參數* @param string $version 版本號* @return array*/private static function getCommonPostRequest($action, array $param = [], $version = "2018-03-01") {//時間戳$timeStamp = time();//$timeStamp ? ? ? = ? 1586333773;//參數轉化Json$paramJson = json_encode($param);//規范請求串$hashedRequestPayload = self::HashEncryption($paramJson);$canonicalRequest = self::HTTPRequestMethod . "\n" .self::CanonicalURI . "\n" .self::CanonicalQueryString . "\n" .self::CanonicalHeaders . "\n" .self::SignedHeaders . "\n" .$hashedRequestPayload;//簽名字符串$date ? ? ? ? ? ?= ? gmdate("Y-m-d", $timeStamp);//UTC 0時區的值$credentialScope = $date . "/" . self::Service . "/" . self::Stop;$hashedCanonicalRequest = self::HashEncryption($canonicalRequest);$stringToSign = self::Algorithm . "\n" .$timeStamp . "\n" .$credentialScope . "\n" .$hashedCanonicalRequest;//計算簽名$secretDate = self::HashHmacSha256Encryption($date, 'TC3' . self::SecretKey);$secretService = self::HashHmacSha256Encryption(self::Service, $secretDate);$secretSigning = self::HashHmacSha256Encryption(self::Stop, $secretService);//簽名$signature = self::HashHmacSha256Encryption($stringToSign, $secretSigning, false);// echo $signature . " \n";$authorization = self::Algorithm . ' ' .'Credential=' . self::SecretId . '/' . $credentialScope . ', ' .'SignedHeaders=' . self::SignedHeaders . ', ' .'Signature=' . $signature;//Header頭部$headers = ["Authorization: $authorization","Host: faceid.tencentcloudapi.com","Content-Type: application/json; charset=utf-8","X-TC-Action: $action","X-TC-Version: $version","X-TC-Timestamp: $timeStamp","X-TC-Region: ap-beijing"];//請求$response = self::get_curl_request(self::Url, $paramJson, self::HTTPRequestMethod, $headers);echo($paramJson);//解析if (!$response) {return ['code' => 0, 'codeError' => '1002', 'msg' => 'Interface request failed'];}$response = json_decode($response, true);if (!isset($response['Response'])) {return ['code' => 0, 'codeError' => '1003', 'msg' => 'Response error'];}if (isset($response['Response']['Error'])) {return ['code' => 0, 'codeError' => $response['Response']['Error']['Code'], 'msg' => $response['Response']['Error']['Message'], 'RequestId' => $response['Response']['RequestId']];} else {return ['code' => 1, 'msg' => 'ok', 'data' => $response['Response']];}}private static function HashEncryption($sign) {return strtolower(hash(self::Algo, $sign));}private static function HashHmacSha256Encryption($sign, $key, $flag = true) {return hash_hmac(self::Algo, $sign, $key, $flag);}/*** @param $url* @param array $param* @param string $mothod* @param array $headers* @param int $return_status* @param int $flag* @return array|bool|string*/public static function get_curl_request($url, $param = [], $mothod = 'POST', $headers = [], $return_status = 0, $flag = 0) {$ch = curl_init();if (!$flag) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);}curl_setopt($ch, CURLOPT_TIMEOUT, 6);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);if (strtolower($mothod) == 'post') {curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $param);} else {$url = $url . "?" . http_build_query($param);}curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 2);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);#curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1");//代理服務器地址#curl_setopt($ch, CURLOPT_PROXYPORT, 12639);//代理服務器端口$ret = curl_exec($ch);$code = curl_getinfo($ch);curl_close($ch);if ($return_status == "1") {return array($ret, $code);}return $ret;}}//執行$model = new Face();$response = $model->getDetectAuth();// 認證前寫進數據庫// 相應信息$requestId = $response["data"]["RequestId"];$bizToken = $response["data"]["BizToken"];// 假設BizToken存在于data中$starttime=time();$certifyip=egetip();$empire->query("insert into {$dbtbpre}member_verify(userid,username,starttime,verifyip,BizToken,RequestId) values('$user[userid]','$name','$starttime','$certifyip','$bizToken','$requestId');");// 然后您可以根據需要使用這些值,比如打印出來// echo "RequestId: " . $requestId . "\n";// echo "BizToken: " . $bizToken . "\n";//?if ($response["code"] == 1) {//獲取到鑒權URL進行跳轉$url = $response["data"]["Url"];// echo($url);//鑒權并且調用人臉核身header("Location: {$url}");} else {printerror2('數據錯誤','/e/member/cp/');}
}
這里同時還可以當用戶注冊人工之后,通知給管理員。