Realname.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\api\model;
  3. use think\Db;
  4. use think\Model;
  5. class Realname extends Model
  6. {
  7. //实名认证
  8. public function realName ($data)
  9. {
  10. $url = "http://op.juhe.cn/idcard/query";
  11. $params = array(
  12. "idcard" => $data['idcard'],//身份证号码
  13. "realname" => $data['user_nickname'],//真实姓名
  14. "key" => "43296fe84447eb0090163d52e7163871",//应用APPKEY(应用详细页查询)
  15. );
  16. $paramstring = http_build_query($params);
  17. $content = self::juhecurl($url,$paramstring);
  18. $result = json_decode($content,true);
  19. if($result){
  20. if($result['error_code']=='0'){
  21. if($result['result']['res'] == '1'){
  22. $updrealname = Db::name('users')->where('user_id',$data['user_id'])->update(
  23. ['is_realname' => 1]
  24. );
  25. if ($updrealname) {
  26. return json(['code' => 200, 'msg' => '实名认证成功']);
  27. } else {
  28. return json(['code' => 100, 'msg' => '实名认证失败']);
  29. }
  30. }else{
  31. return json(['code' => 100,'msg' => '身份证号码和真实姓名不一致','data' => []]);
  32. }
  33. #print_r($result);
  34. }else{
  35. return json(['code' => 100,'msg' => $result['error_code'].":".$result['reason'],'data' => []]);
  36. }
  37. }else{
  38. return json(['code' => 100,'msg' => '请求失败','data' => []]);
  39. }
  40. }
  41. /**
  42. * 请求接口返回内容
  43. * @param string $url [请求的URL地址]
  44. * @param string $params [请求的参数]
  45. * @param int $ipost [是否采用POST形式]
  46. * @return string
  47. */
  48. public function juhecurl($url,$params=false,$ispost=0){
  49. $httpInfo = array();
  50. $ch = curl_init();
  51. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  52. curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
  53. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
  54. curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
  55. curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  56. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  57. if( $ispost )
  58. {
  59. curl_setopt( $ch , CURLOPT_POST , true );
  60. curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  61. curl_setopt( $ch , CURLOPT_URL , $url );
  62. }
  63. else
  64. {
  65. if($params){
  66. curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  67. }else{
  68. curl_setopt( $ch , CURLOPT_URL , $url);
  69. }
  70. }
  71. $response = curl_exec( $ch );
  72. if ($response === FALSE) {
  73. //echo "cURL Error: " . curl_error($ch);
  74. return false;
  75. }
  76. $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  77. $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  78. curl_close( $ch );
  79. return $response;
  80. }
  81. }