PHP識別身份證圖片(呼叫阿里雲市場介面),涵數來源:夢行雲軟體
阿新 • • 發佈:2020-10-07
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; }