用PHP调用聚合数据证件识别接口,识别本地图片

巴扎黑
巴扎黑 原创
2016-11-07 17:27:29 1108浏览

本文章讲的是如何通过PHP调用聚合数据的证件识别接口

前置条件

1 在开始前,请作如下准备

学会用PHP输出“Hello World”

去聚合数据申请证件识别专用的KEY:https://www.juhe.cn/docs/api/id/153

操作步骤

配置好PHP开发环境

在相应的本地网站根目录下新建一个文件夹并命名为:card

请准备一张jpg格式的身份证照片(本示例中的图片来自网络),并命名为1.jpg,放在card目录

请务必确保PHP对1.jpg有读权限(先用fopen('1.jpg', 'r')测试一下)

在card目录新建一个index.php文件,并输入以下内容:

Php代码

= 5.5.0)
 * 示例中的身份证图片来自网络,用真实的身份证图片会有更佳的识别效果
 */
 
header("Content-type:text/html;charset=utf-8");
$config = array(
   'key' => '将我替换成您申请的KEY',
   //聚合数据证件识别接口的URL地址
   'url' => 'http://v.juhe.cn/certificates/query.php',
   //证件的类型,这里是身份证正面
   'type' => 'image/jpg',
   //证件图片的类型
   'cardType' => '2',
);
 
/*第一种方式*/
$ch = curl_init($config['url']);
//$filename 

Path to the file which will be uploaded.

//$postname [optional]

Name of the file.

$cfile = curl_file_create('filename.jpg', $config['type'], 'postname.jpg'); $data = array( 'cardType' => $config['cardType'], 'key' => $config['key'], 'pic' => $cfile, ); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //已经获取到内容,还没输出,如果不加下面这行,则不需要echo response //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); curl_close($ch); /*/第一种方式*/ /*第二种方式*/ $data = array( 'cardType' => $config['cardType'], 'key' => $config['key'], 'pic' => "@1.jpg", ); post($config['url'], $data); /*/第二种方式*/ function post($url, $data) { $ch = curl_init(); curl_setopt( $ch , CURLOPT_POST , true ); @curl_setopt( $ch , CURLOPT_POSTFIELDS , $data); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); echo $response; }

6.打开浏览器,访问http://localhost/card/index.php,正常情况下你应该看到类似下面的内容:

Php代码

{"error_code":"200","reason":"操作成功","result":{"住址":"XX省XX县XX村XX号","保留":"","公民身份号码":"420188195408288888","出生":"1954-08-28","头像":"","姓名":"XXX","性别":"女","民族":"汉族"}}
{"error_code":"200","reason":"操作成功","result":{"住址":"XX省XX县XX村XX号","保留":"","公民身份号码":"420188195408288888","出生":"1954-08-28","头像":"","姓名":"XXX","性别":"女","民族":"汉族"}}

7.如果PHP版本低于5.5,但是又想用curl_file_create,请参考官方文档提供的方法:http://php.net/manual/en/function.curl-file-create.php

Php代码

For PHP < 5.5:
 


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。