腾讯云人脸识别及获取签名方法是什么?

Katen Doe

他的问题描述

按照腾讯云人脸识别,身份证识别签名总是错误,我也摸不着头绪,客服也没有解答具体原因?

腾讯云存储、识别签名失败的看这里.... 以下是PHP操作的具体方法

class Face{
    const SecretId  = "AKIDb1BxSgdLmwqUrxmKcdNq3ze2A2dy2pLb";
    const SecretKey = "0kDPKyOccMq43mFSQ8aonWzDBtQmsiJg";
    //const Url       = "https://ocr.tencentcloudapi.com";
    const Url       = "https://iai.tencentcloudapi.com";

    //算法
    const Algo                  = "sha256";
    //规范请求串
    const HTTPRequestMethod     = "POST";
    const CanonicalURI          = "/";
    const CanonicalQueryString  = "";
    const CanonicalHeaders      = "content-type:application/json; charset=utf-8
host:iai.tencentcloudapi.com
";
    const SignedHeaders         = "content-type;host";//参与签名的头部信息

    //签名字符串
    const Algorithm             = "TC3-HMAC-SHA256";
    const Service               = "iai";
    //const Service               = "ocr";
    const Stop                  = "tc3_request";

  
    public function getFace(){
        $param = [
            
           // 'ImageUrl'=>"https://ocr-demo-1254418846.cos.ap-guangzhou.myqcloud.com/card/IDCardOCR/IDCardOCR1.jpg",
           // 'CardSide' => 'FRONT' IDCardOCR
		   
		   'GroupId' => 'ipdr20220126',
           'PersonName' => 'Junly',
           'PersonId' => '2001',
           'Gender' =>1,
		   'Url'=>"https://weiyum-1257789814.cos.ap-shanghai.myqcloud.com/o7E6w4r6C8YdB8fTC8XSNJWn2A4Qipdr89.jpg"
           
            
        ];
        return self::getCommonPostRequest("CreatePerson", $param,"2020-03-03","https://iai.tencentcloudapi.com");
    }

    /**
     * 鉴权
     * @param string $action 方法
     * @param array  $param  参数
     * @param string $version 版本号
     * @return array
     */
    private static function getCommonPostRequest( $action, array $param = [], $version = "2018-11-19",$url)
    {
        //时间戳
        $timeStamp  = time();
        //参数转化Json
        $paramJson  = json_encode($param);
        //规范请求串
        $hashedRequestPayload   =   self::HashEncryption($paramJson);
        $canonicalRequest       =   self::HTTPRequestMethod . "
" .
            self::CanonicalURI . "
" .
            self::CanonicalQueryString . "
" .
            self::CanonicalHeaders . "
" .
            self::SignedHeaders . "
" .
            $hashedRequestPayload;
        //签名字符串
        $date                   =   gmdate("Y-m-d", $timeStamp);//UTC 0时区的值
        $credentialScope        =   $date . "/". self::Service . "/". self::Stop;
        $hashedCanonicalRequest =   self::HashEncryption($canonicalRequest);
        $stringToSign           =   self::Algorithm . "
" .
            $timeStamp . "
" .
            $credentialScope . "
" .
            $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);
        $authorization          =   self::Algorithm . ' ' .
            'Credential=' . self::SecretId . '/' . $credentialScope . ', ' .
            'SignedHeaders=' . self::SignedHeaders . ', ' .
            'Signature=' . $signature;

        //Header头部
        $headers = [
            "Authorization: $authorization",
            "Host: iai.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($url, $paramJson, self::HTTPRequestMethod, $headers);
        //解析
        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_PROXY, "127.0.0.1");
        //curl_setopt($ch, CURLOPT_PROXYPORT, 12639);

        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);
        $ret = curl_exec($ch);
        $code = curl_getinfo($ch);
        curl_close($ch);
        if ($return_status == "1") {
            return array($ret, $code);
        }
        return $ret;
    }
}

$model = new Face();
$data = $model->getFace();
var_dump($data);


...
A caption for the above image.

解决回答 (3)

wave
  • John Doe

    John Doe

    Jan 08, 2021 14:41 pm

    腾讯云存储、识别签名失败的看这里.... 以下是PHP操作的具体方法

    class Face{
        const SecretId  = "AKIDb1BxSgdLmwqUrxmKcdNq3ze2A2dy2pLb";
        const SecretKey = "0kDPKyOccMq43mFSQ8aonWzDBtQmsiJg";
        //const Url       = "https://ocr.tencentcloudapi.com";
        const Url       = "https://iai.tencentcloudapi.com";
    
        //算法
        const Algo                  = "sha256";
        //规范请求串
        const HTTPRequestMethod     = "POST";
        const CanonicalURI          = "/";
        const CanonicalQueryString  = "";
        const CanonicalHeaders      = "content-type:application/json; charset=utf-8
    host:iai.tencentcloudapi.com
    ";
        const SignedHeaders         = "content-type;host";//参与签名的头部信息
    
        //签名字符串
        const Algorithm             = "TC3-HMAC-SHA256";
        const Service               = "iai";
        //const Service               = "ocr";
        const Stop                  = "tc3_request";
    
      
        public function getFace(){
            $param = [
                
               // 'ImageUrl'=>"https://ocr-demo-1254418846.cos.ap-guangzhou.myqcloud.com/card/IDCardOCR/IDCardOCR1.jpg",
               // 'CardSide' => 'FRONT' IDCardOCR
    		   
    		   'GroupId' => 'ipdr20220126',
               'PersonName' => 'Junly',
               'PersonId' => '2001',
               'Gender' =>1,
    		   'Url'=>"https://weiyum-1257789814.cos.ap-shanghai.myqcloud.com/o7E6w4r6C8YdB8fTC8XSNJWn2A4Qipdr89.jpg"
               
                
            ];
            return self::getCommonPostRequest("CreatePerson", $param,"2020-03-03","https://iai.tencentcloudapi.com");
        }
    
        /**
         * 鉴权
         * @param string $action 方法
         * @param array  $param  参数
         * @param string $version 版本号
         * @return array
         */
        private static function getCommonPostRequest( $action, array $param = [], $version = "2018-11-19",$url)
        {
            //时间戳
            $timeStamp  = time();
            //参数转化Json
            $paramJson  = json_encode($param);
            //规范请求串
            $hashedRequestPayload   =   self::HashEncryption($paramJson);
            $canonicalRequest       =   self::HTTPRequestMethod . "
    " .
                self::CanonicalURI . "
    " .
                self::CanonicalQueryString . "
    " .
                self::CanonicalHeaders . "
    " .
                self::SignedHeaders . "
    " .
                $hashedRequestPayload;
            //签名字符串
            $date                   =   gmdate("Y-m-d", $timeStamp);//UTC 0时区的值
            $credentialScope        =   $date . "/". self::Service . "/". self::Stop;
            $hashedCanonicalRequest =   self::HashEncryption($canonicalRequest);
            $stringToSign           =   self::Algorithm . "
    " .
                $timeStamp . "
    " .
                $credentialScope . "
    " .
                $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);
            $authorization          =   self::Algorithm . ' ' .
                'Credential=' . self::SecretId . '/' . $credentialScope . ', ' .
                'SignedHeaders=' . self::SignedHeaders . ', ' .
                'Signature=' . $signature;
    
            //Header头部
            $headers = [
                "Authorization: $authorization",
                "Host: iai.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($url, $paramJson, self::HTTPRequestMethod, $headers);
            //解析
            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_PROXY, "127.0.0.1");
            //curl_setopt($ch, CURLOPT_PROXYPORT, 12639);
    
            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);
            $ret = curl_exec($ch);
            $code = curl_getinfo($ch);
            curl_close($ch);
            if ($return_status == "1") {
                return array($ret, $code);
            }
            return $ret;
        }
    }
    
    $model = new Face();
    $data = $model->getFace();
    var_dump($data);


    给他点赞
我来回答TA

 

Press ESC to close