Idcard.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. return $v['data'];
  51. }
  52. }else{
  53. Dever::alert('身份证背面已过期');
  54. }
  55. }
  56. // if(isset($rest['face']) && $rest['face']){
  57. // foreach($rest as $k=>$v){
  58. // if($name != $v['data']['name'] || $idcard != $v['data']['idNumber']){
  59. // Dever::alert('姓名或身份证号不匹配');
  60. // }
  61. // }
  62. // }elseif(isset($rest['back']) && $rest['back']){
  63. // foreach($rest as $k => $v){
  64. // $date = explode('-',$v['data']['validPeriod']);
  65. // $cdate = strtotime(str_replace('.','-',$date[1]));
  66. // if(time()>=$cdate){
  67. // Dever::alert('身份证背面已过期');
  68. // }
  69. // }
  70. // }
  71. // return $rest;
  72. }else{
  73. Dever::alert('身份证图片有误');
  74. }
  75. }
  76. private static function getCommonParams(){
  77. date_default_timezone_set("GMT");
  78. $data = [
  79. 'Format' => 'JSON',
  80. 'Version' => '2021-07-07',
  81. 'AccessKeyId' => self::$AccessKeyId,
  82. 'SignatureMethod' => 'HMAC-SHA1',
  83. 'Timestamp' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z',//'2021-08-09T08:27:00Z',//,
  84. 'SignatureVersion' => '1.0',
  85. 'SignatureNonce' => uniqid(),//microtime(),//
  86. 'Action' => 'DescribeRegions',
  87. ];
  88. return $data;
  89. }
  90. private static function percentEncode($str)
  91. {
  92. // 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范
  93. $res = urlencode($str);
  94. $res = preg_replace('/\+/', '%20', $res);
  95. $res = preg_replace('/\*/', '%2A', $res);
  96. $res = preg_replace('/%7E/', '~', $res);
  97. return $res;
  98. }
  99. //生成签名
  100. private function getSignature($parameters)
  101. {
  102. // 将参数Key按字典顺序排序
  103. ksort($parameters);
  104. // 生成规范化请求字符串
  105. $canonicalizedQueryString = '';
  106. foreach($parameters as $key => $value)
  107. {
  108. $canonicalizedQueryString .= '&' .self::percentEncode($key)
  109. . '=' . self::percentEncode($value);
  110. }
  111. // 生成用于计算签名的字符串 stringToSign
  112. $stringToSign = 'GET&%2F&' . self::percentEncode(substr($canonicalizedQueryString, 1));
  113. // 计算签名,注意accessKeySecret后面要加上字符'&'
  114. $signature = self::percentEncode(base64_encode(hash_hmac('sha1', $stringToSign, self::$AccessKeySecret . '&', true)));
  115. return $signature;
  116. }
  117. }