123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace Setting\Lib;
- use Dever;
- class Idcard
- {
- static $AccessKeyId = 'LTAI5t5k4M2gK6kctX6iq8CT';
- static $AccessKeySecret = 'hfFy8smTc9CKEThpqFTG32zJuHd3dS';
- static $AliHost = 'http://ocr-api.cn-hangzhou.aliyuncs.com/?';
-
- public function getIdcard_api(){
- $param = self::getCommonParams();
- $param['Action'] = 'RecognizeIdcard';
- $param['OutputFigure'] = 'false';
- $param['Url'] = Dever::input('url');
- $name = Dever::input('name');
- $idcard = Dever::input('idcard');
- $type = Dever::input('type');
- $param['Signature'] = self::getSignature($param);
- $url = 'https://ocr-api.cn-hangzhou.aliyuncs.com/?';
- foreach ($param as $k => $v) {
- $url .= '&' . $k . '=' . $v;
- }
- $data = Dever::load('setting/lib/idcard')->callInterface($url,$name,$idcard,$type);
- return $data;
- }
-
-
- public function callInterface($url,$name,$idcard,$type){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $res = curl_exec($ch);
- curl_close($ch);
- $res =(array)(json_decode($res));
- if($res && isset($res['Data']) && $res['Data']){
- $data = (array)(json_decode($res['Data'],true));
- $rest = (array)($data['data']);
- if($type == 1){
- if(isset($rest['face']) && $rest['face']){
- foreach($rest as $k => $v){
- return $v['data'];
- }
- }else{
- Dever::alert('请上传正确的身份证正面图片');
- }
- }elseif($type == 2){
- if(isset($rest['back']) && $rest['back']){
- foreach($rest as $k =>$v){
- if (isset($v['data']['validPeriod'])) {
- $date = explode('-',$v['data']['validPeriod']);
- if($date[1] && $date[1] != '长期'){
- $cdate = strtotime(str_replace('.','-',$date[1]));
- if(time()>=$cdate){
- Dever::alert('身份证背面已过期');
- }
- }
-
- } else {
- Dever::alert('请上传正确的身份证背面图片');
- }
- return $v['data'];
- }
- }else{
- Dever::alert('请上传正确的身份证背面图片');
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }else{
- Dever::alert('身份证图片有误');
- }
- }
- private static function getCommonParams(){
- date_default_timezone_set("GMT");
- $data = [
- 'Format' => 'JSON',
- 'Version' => '2021-07-07',
- 'AccessKeyId' => self::$AccessKeyId,
- 'SignatureMethod' => 'HMAC-SHA1',
- 'Timestamp' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z',
- 'SignatureVersion' => '1.0',
- 'SignatureNonce' => uniqid(),
- 'Action' => 'DescribeRegions',
- ];
-
- return $data;
- }
- private static function percentEncode($str)
- {
-
- $res = urlencode($str);
- $res = preg_replace('/\+/', '%20', $res);
- $res = preg_replace('/\*/', '%2A', $res);
- $res = preg_replace('/%7E/', '~', $res);
- return $res;
- }
-
- private function getSignature($parameters)
- {
-
- ksort($parameters);
-
- $canonicalizedQueryString = '';
- foreach($parameters as $key => $value)
- {
- $canonicalizedQueryString .= '&' .self::percentEncode($key)
- . '=' . self::percentEncode($value);
- }
-
- $stringToSign = 'GET&%2F&' . self::percentEncode(substr($canonicalizedQueryString, 1));
-
- $signature = self::percentEncode(base64_encode(hash_hmac('sha1', $stringToSign, self::$AccessKeySecret . '&', true)));
- return $signature;
- }
- }
|