“利用PHP识别是假的,封装的接口到是真的”,emmmm.... 是的,就是这个意思,只不过封装的接口而已...
许久未更新博客,这两个函数之前写东西时搞的,分享出来做做水文吧。
1.身份证识别
接口来自于 腾讯AI开放平台 (免费),用自己的QQ登录一个平台,然后创建一个应用,审核之后得到app_id和key参数,再配置一下即可使用。
Ps:应用创建之后是需要审核的,大概一天左右吧;如果不想创建,复制下面代码直接用我的也行,但我不保证长期能用哈,所以还是推荐自己搞一个。
参数:card_type代表正反面;
身份证识别代码:
<?php
function get_curl($url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0, $async = 0) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpheader[] = "Accept:*/*";
$httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
$httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
$httpheader[] = "Connection:close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
if ($async) {
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($header) {
curl_setopt($ch, CURLOPT_HEADER, true);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($referer) {
if ($referer == 1) {
curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
} else {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
}
if ($ua) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
}
if ($nobaody) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
//本站签名算法
function getReqSign($params /* 关联数组 */ , $appkey /* 字符串*/ ) {
// 1. 字典升序排序
ksort($params);
// 2. 拼按URL键值对
$str = '';
foreach ($params as $key => $value) {
if ($value !== '') {
$str .= $key . '=' . urlencode($value) . '&';
}
}
// 3. 拼接app_key
$str .= 'app_key=' . $appkey;
// 4. MD5运算+转换大写,得到请求签名
$sign = strtoupper(md5($str));
return $sign;
}
/* 身份证照片识别,API提取自腾讯OCR */
function SFZ_OCR_($path) {
if (!isset($path))
return false;
//腾讯AI开放平台
$app_id = 2113119487;
$key = 'fWA7BjyR3e4USb2B';
$api = "https://api.ai.qq.com/fcgi-bin/ocr/ocr_idcardocr";
$post['app_id'] = $app_id;
$post['time_stamp'] = intval(time());
$post['nonce_str'] = time();
$post['card_type'] = 0; //正面
$post['image'] = base64_encode(file_get_contents($path));
$post['sign'] = getReqSign($post, $key);
$ret = get_curl($api, $post);
return $ret;
}
$path = 'https://open.youtu.qq.com/static/img/icon_id_02.26ed156.jpg';
echo "<img src='$path?" . time() . "' /><br/><br/>";
$ret = json_decode(SFZ_OCR_($path), true);
print_r($ret);
效果图:
简单讲解:get_curl() http请求函数,getReqSign();签名函数【重要】,SFZ_OCR_() 接口请求函数,函数返回的是json,所有需要Json_decode才能看到数组。
官方文档:https://ai.qq.com/doc/ocridcardocr.shtml
2.二维码识别
接口是抓自草料二维码识别,函数传入的二维码URL地址,暂不支持本地二维码。
<?php
/* 二维码图片识别,草料API提取自草料 */
function qrcode_OCR($img) {
$api = "https://cli.im/apis/up/deqrimg";
$post = 'img=' . urlencode($img);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpheader[] = "X-Requested-With: XMLHttpRequest";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
$qrcode = 'http://qr.topscan.com/api.php?text=https://qzone.work/';
echo "<img src='$qrcode?" . time() . "' /><br/><br/>";
$ret = json_decode(qrcode_OCR($qrcode), true);
print_r($ret);
效果图:
函数返回同样是 String json,需要json_decode();