PHP识别身份证图片(调用阿里云市场接口),涵数来源:梦行云软件

function get_idcard_info($img_path,$appcode){//识别身份证图片
    $r=array('state'=>'fail','msg'=>'图片不存在',);
    if(!is_file($img_path)){return $r;}
    $host = "https://ocridcard.market.alicloudapi.com";
    $path = "/idimages";
    $method = "POST";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    //根据API的要求,定义相对应的Content-Type
    array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
    $querys = "";
      $bodys = 'image='.imgToBase64($img_path).'&idCardSide=front';   //png图片base64 +  正反面参数 默认正面,背面请传back
    //var_dump($bodys);
    $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);
    curl_setopt($curl, CURLOPT_HEADER, false);
    //curl_setopt($curl, CURLOPT_HEADER, true);   如不输出json, 请打开这行代码,打印调试头部状态码。
    //状态码: 200 正常;400 URL无效;401 appCode错误; 403 次数用完; 500 API网管错误
    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);
    $out_put = curl_exec($curl);

    $t=explode('{"code"', $out_put);
    if(!isset($t[1])){$r['state']='fail';return $r;}

    $v='{"code"'.$t[1];
    $r=json_decode($v,1);
    if($r['code']!=1){$r['state']='fail';return $r;}
    if($r['result']['code']==''){$r['state']='fail';return $r;}
    $msg=$r['msg'];
    $r=$r['result'];
    $r['state']='success';
    $r['msg']=$msg;
    return $r;
}

 

posted @ 2020-10-07 19:39  Monxin梦行云软件  阅读(481)  评论(0)    收藏  举报