test 发表于 2017-7-4 18:56:18

阿里云身份证识别接口开发

一,图片转换为base64
    function image_base64($image_file) {
      $file=$image_file;
      $type=getimagesize($file);//取得图片的大小,类型等
      $fp=fopen($file,"r")or die("Can't open file");
      $file_content=chunk_split(base64_encode(fread($fp,filesize($file))));//base64编码
      fclose($fp);
      return $file_content;
    }
二,定义图片地址
    $image_file = '';
    $pic = image_base64($image_file);
三,执行接口查询
    $host = "https://dm-51.data.aliyun.com";
    $path = "/rest/160601/ocr/ocr_idcard.json";
    $method = "POST";
    $appcode = "";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    //根据API的要求,定义相对应的Content-Type
    array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
    $querys = "";
    $bodys = array(
            "inputs"=>array(
                array(
                  "image"=>array(
                            "dataType"=>50,
                            "dataValue"=>$pic
                        )
                  ,
                  "configure"=>array(
                            "dataType"=>50,
                            "dataValue"=> json_encode(array("side"=>"face"))
                        )
                ))
      );
    $bodys = json_encode($bodys);
// var_dump($bodys);//exit;
    $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, true);
    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);
    var_dump(curl_exec($curl));
老话如果您还是搞不定请注册本站吧10钱帮你搞定

本网站发布的修改方法为本人原创,转载请注明出处:www.sssi.cn
页: [1]
查看完整版本: 阿里云身份证识别接口开发