Idcard.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Setting\Lib;
  3. use Dever;
  4. class Idcard
  5. {
  6. static $AccessKeyId = 'LTAI5t5k4M2gK6kctX6iq8CT';
  7. static $AccessKeySecret = 'hfFy8smTc9CKEThpqFTG32zJuHd3dS';
  8. static $AliHost = 'http://ocr-api.cn-hangzhou.aliyuncs.com/?';
  9. //查询域名
  10. public function getIdcard_api(){
  11. $param = self::getCommonParams();
  12. $param['Action'] = 'RecognizeIdcard';
  13. $param['OutputFigure'] = 'false';
  14. $param['Url'] = Dever::input('url');
  15. $name = Dever::input('name');
  16. $idcard = Dever::input('idcard');
  17. $type = Dever::input('type');#1=正面,2=反面
  18. $param['Signature'] = self::getSignature($param);
  19. $url = 'https://ocr-api.cn-hangzhou.aliyuncs.com/?';
  20. foreach ($param as $k => $v) {
  21. $url .= '&' . $k . '=' . $v;
  22. }
  23. $data = Dever::load('setting/lib/idcard')->callInterface($url,$name,$idcard,$type);
  24. return $data;
  25. }
  26. // http://guanli.uat.churenyiliao.com/data/upload/8/2022/06/09/71698f0e0b574317363c8a61409585cd_w1.jpg
  27. // http%3A%2F%2Fguanli.uat.churenyiliao.com%2Fdata%2Fupload%2F9%2F2022%2F06%2F08%2Ffe5f8b472eb2fd76d144f926c0e2b725.jpg
  28. public function callInterface($url,$name,$idcard,$type){
  29. $ch = curl_init();
  30. curl_setopt($ch, CURLOPT_URL, $url);
  31. curl_setopt($ch, CURLOPT_HEADER, 0);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  33. $res = curl_exec($ch);
  34. curl_close($ch);
  35. $res =(array)(json_decode($res));
  36. if($res && isset($res['Data']) && $res['Data']){
  37. $data = (array)(json_decode($res['Data'],true));
  38. $rest = (array)($data['data']);
  39. if($type == 1){
  40. if(isset($rest['face']) && $rest['face']){
  41. foreach($rest as $k => $v){
  42. return $v['data'];
  43. }
  44. }else{
  45. Dever::alert('请上传正确的身份证正面图片');
  46. }
  47. }elseif($type == 2){
  48. if(isset($rest['back']) && $rest['back']){
  49. foreach($rest as $k =>$v){
  50. if (isset($v['data']['validPeriod'])) {
  51. $date = explode('-',$v['data']['validPeriod']);
  52. if($date[1] && $date[1] != '长期'){
  53. $cdate = strtotime(str_replace('.','-',$date[1]));
  54. if(time()>=$cdate){
  55. Dever::alert('身份证背面已过期');
  56. }
  57. }
  58. } else {
  59. Dever::alert('请上传正确的身份证背面图片');
  60. }
  61. return $v['data'];
  62. }
  63. }else{
  64. Dever::alert('请上传正确的身份证背面图片');
  65. }
  66. }
  67. // if(isset($rest['face']) && $rest['face']){
  68. // foreach($rest as $k=>$v){
  69. // if($name != $v['data']['name'] || $idcard != $v['data']['idNumber']){
  70. // Dever::alert('姓名或身份证号不匹配');
  71. // }
  72. // }
  73. // }elseif(isset($rest['back']) && $rest['back']){
  74. // foreach($rest as $k => $v){
  75. // $date = explode('-',$v['data']['validPeriod']);
  76. // $cdate = strtotime(str_replace('.','-',$date[1]));
  77. // if(time()>=$cdate){
  78. // Dever::alert('身份证背面已过期');
  79. // }
  80. // }
  81. // }
  82. // return $rest;
  83. }else{
  84. Dever::alert('身份证图片有误');
  85. }
  86. }
  87. private static function getCommonParams(){
  88. date_default_timezone_set("GMT");
  89. $data = [
  90. 'Format' => 'JSON',
  91. 'Version' => '2021-07-07',
  92. 'AccessKeyId' => self::$AccessKeyId,
  93. 'SignatureMethod' => 'HMAC-SHA1',
  94. 'Timestamp' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z',//'2021-08-09T08:27:00Z',//,
  95. 'SignatureVersion' => '1.0',
  96. 'SignatureNonce' => uniqid(),//microtime(),//
  97. 'Action' => 'DescribeRegions',
  98. ];
  99. return $data;
  100. }
  101. private static function percentEncode($str)
  102. {
  103. // 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范
  104. $res = urlencode($str);
  105. $res = preg_replace('/\+/', '%20', $res);
  106. $res = preg_replace('/\*/', '%2A', $res);
  107. $res = preg_replace('/%7E/', '~', $res);
  108. return $res;
  109. }
  110. //生成签名
  111. private function getSignature($parameters)
  112. {
  113. // 将参数Key按字典顺序排序
  114. ksort($parameters);
  115. // 生成规范化请求字符串
  116. $canonicalizedQueryString = '';
  117. foreach($parameters as $key => $value)
  118. {
  119. $canonicalizedQueryString .= '&' .self::percentEncode($key)
  120. . '=' . self::percentEncode($value);
  121. }
  122. // 生成用于计算签名的字符串 stringToSign
  123. $stringToSign = 'GET&%2F&' . self::percentEncode(substr($canonicalizedQueryString, 1));
  124. // 计算签名,注意accessKeySecret后面要加上字符'&'
  125. $signature = self::percentEncode(base64_encode(hash_hmac('sha1', $stringToSign, self::$AccessKeySecret . '&', true)));
  126. return $signature;
  127. }
  128. }