/**
* 证件识别接口示例
* 提供两种方式,请根据您的PHP版本、服务器环境等因素选择适合的方式
* 推荐使用第一种(PHP 5 >= 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 <p> Path to the file which will be uploaded.</p>
//$postname [optional] <p>Name of the file.</p>
$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,正常情况下你应该看到类似下面的内容:{"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.phpFor PHP < 5.5:
<?php
if (!function_exists('curl_file_create')) {
function curl_file_create($filename, $mimetype = '', $postname = '') {
return "@$filename;filename="
. ($postname ?: basename($filename))
. ($mimetype ? ";type=$mimetype" : '');
}
}
?>
ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。