腾讯云人脸核身pc端H5接入 php类

腾讯云人脸核身pc端H5接入 php类

app_id = $config['app_id'];$this->secret = $config['secret'];}/*** 修改回调地址*/public function setCallbackUrl($callbackUrl){$this->callbackUrl = $callbackUrl;}/*** 合作方后台上传身份信息* 并返回h5人脸核身链接* $orderNo  订单编码* $name  姓名* $idNo  身份证号* $userId  用户ID*/public function getH5Faceid($orderNo, $name, $idNo, $userId){$post_url = $this->getH5FaceidUrl.'?orderNo='.$orderNo;$sign_ticket = $this->getSignTicket();$sign = $this->getSign($orderNo, $name, $idNo, $userId, $sign_ticket);$post = ['webankAppId' => $this->app_id,'orderNo' => $orderNo,'name' => $name,'idNo' => $idNo,'userId' => $userId,'version' => $this->version,'sign' => $sign,];$client = new Client();$response = $client->request('POST', $post_url, ['json'  => $post,])->getBody()->getContents();$result = json_decode($response, true);$optimalDomain = $result['result']['optimalDomain'];$h5faceId = $result['result']['h5faceId'];$nonce = $this->getNonce(32);$url = $this->getH5Url($optimalDomain, $orderNo, $userId, $h5faceId, $sign_ticket, $nonce);return $url;}/*** 生成签名并拼接人脸核身url*/private function getH5Url($optimalDomain, $orderNo, $userId, $h5faceId, $ticket, $nonce){$arr = [$this->app_id,$orderNo,$userId,$this->version,$h5faceId,$ticket,$nonce];sort($arr);$str = implode('', $arr);$sign = sha1($str);$url = 'https://'.$optimalDomain.'/api/pc/login?'.'webankAppId='.$this->app_id.'&version='.$this->version.'&nonce='.$nonce.'&orderNo='.$orderNo.'&h5faceId='.$h5faceId.'&url='.$this->callbackUrl.'&userId='.$userId.'&sign='.$sign;return $url;}/*** 获取随机数*/private function getNonce($num){$str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";$key = "";for($i=0;$i<$num;$i++) {$key .= $str{mt_rand(0,32)};    //生成php随机数}return $key;}/*** 获取sign签名*/private function getSign($orderNo, $name, $idNo, $userId, $sign_ticket){$arr = [$this->app_id,$orderNo,$name,$idNo,$userId,$this->version,$sign_ticket];sort($arr);$str = implode('', $arr);return sha1($str);}/*** 获取 SIGN ticket* 文档: https://cloud.tencent.com/document/product/1007/37305*/private function getSignTicket(){if(file_exists(public_path().'/sign_ticket.txt')){$data = file_get_contents(public_path().'/sign_ticket.txt');$data_arr = json_decode($data, true);}if (!isset($data_arr['expire_time']) || $data_arr['expire_time']getSignTicketUrl.'?app_id='.$this->app_id.'&access_token='.$this->getAccessToken().'&type=SIGN&version='.$this->version;$data = file_get_contents($url);$data_arr = json_decode($data, true);$data_arr = ['expire_time' => time()+3600,'sign_ticket' => $data_arr['tickets'][0]['value'],];file_put_contents(public_path().'/sign_ticket.txt', json_encode($data_arr, JSON_UNESCAPED_UNICODE));}return $data_arr['sign_ticket'];}/*** 获取 access_token* 文档: https://cloud.tencent.com/document/product/1007/37304*/private function getAccessToken(){if(file_exists(public_path().'/access_token.txt')){$data = file_get_contents(public_path().'/access_token.txt');$data_arr = json_decode($data, true);}if (!isset($data_arr['expire_time']) || $data_arr['expire_time']getAccessTokenUrl.'?app_id='.$this->app_id.'&secret='.$this->secret.'&grant_type='.$this->grant_type.'&version='.$this->version;$data = file_get_contents($url);$data_arr = json_decode($data, true);file_put_contents(public_path().'/access_token.txt', json_encode(['expire_time' => time()+7200,'access_token' => $data_arr['access_token'],], JSON_UNESCAPED_UNICODE));}return $data_arr['access_token'];}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部