PHP 蚂蚁芝麻信用分接口

[php]  view plain  copy
  1.   
[php]  view plain  copy
  1. <?php  
  2.   
  3. //私钥和公钥在芝麻信用后台设置,官网有详细说明  
  4. class ZhimaAction extends CommonAction {  
  5.         //芝麻信用网关地址  
  6.         public $gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do";  
  7.         //商户私钥文件  
  8.         public $privateKeyFile = "商户私钥文件(绝对路径)";  
  9.         //芝麻公钥文件  
  10.         public $zmPublicKeyFile = "芝麻公钥文件(绝对路径)";  
  11.         //数据编码格式  
  12.         public $charset = "UTF-8";  
  13.         //应用id  
  14.         public $app_id = "*******";  
  15.         //要调用的接口名  
  16.         public $method = "zhima.credit.score.get";  
  17.         //来源平台,默认为zmop  
  18.         public $platform = "zmop";  
  19.           
  20.         //接口版本,目前只支持1.0  
  21.         public $version = "1.0";  
  22.           
  23.         //  加密后信息  RSA加密后的业务参数  
  24.         public $params = "";  
  25.           
  26.         //  加密后信息  对params参数加密前的签名,算法为SHA1WithRSA  
  27.         public $sign = "1.0";  
  28.           
  29. /*         加签过程 
  30.  
  31.         1、在加密过程的第一步,我们得到了拼接在一起的业务参数,同样以芝麻信用评分为例,拼接的参数如下: 
  32.  
  33.         transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496) 
  34.         2、使用 SHA1WithRSA 算法以及商户自己的私钥进行签名,得到 byte 数组 
  35.  
  36.         SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496)) 
  37.         3、将 byte 数组进行 Base64 编码,得到一个签名的字符串 
  38.  
  39.         Base64(SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496))) 
  40.         经过了上述三步,我们便得到了业务参数的签名,最后我们将这个签名的值放入系统参数 sign 中: 
  41.  
  42.         sign=Base64(SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496))) 
  43.         解密和验签 
  44.          
  45.          */  
  46.           
  47.     public function __construct() {  
  48.         parent::__construct();  
  49.       
  50.                 //在官网下载  
  51.             Vendor('zhima.zmop.ZmopClient');  
  52.             Vendor('zhima.zmop.RSAUtil');  
  53.             Vendor('zhima.zmop.ZhimaCreditIvsDetailGetRequest');  
  54.             Vendor('zhima.ZmopSdk');  
  55.     }  
  56.     public function grant(){  
  57.         if(IS_POST){  
  58.             $name = I('name');//姓名  
  59.             $IDnumber = I('IDnumber');  //×××号码  
  60.               
  61.               
  62.              $client = new ZmopClient($this->gatewayUrl,$this->app_id,$this->charset,$this->privateKeyFile,$this->zmPublicKeyFile);  
  63.              $RSAUtil = new RSAUtil();  
  64.               
  65.             $identity_type ='2';  
  66.             $identity_param =json_encode(array('certNo'=>$IDnumber,'name'=>$name,'certType'=>"IDENTITY_CARD"));  
  67.             $request['app_id'] = $this->app_id;  
  68.             $request['charset'] = $this->charset;  
  69.             $request['method'] = 'zhima.auth.info.authorize';  
  70.             $request['version'] = $this->version;  
  71.             $request['platform'] = $this->platform;  
  72.             $request['params'] = $RSAUtil->rsaEncrypt($str,$this->zmPublicKeyFile);  
  73.             $request['sign'] = $RSAUtil->sign($str,$this->privateKeyFile);  
  74.             $request['identity_type'] = $identity_type;  
  75.             $request['identity_param'] = $identity_param;  
  76.    
  77.             $str ='identity_type='.urlencode($identity_type).'&identity_param='.urlencode($identity_param).'';  
  78.             $request = new ZhimaAuthInfoAuthorizeRequest ();  
  79.             $request->setIdentityType ("2");  
  80.         //  $request->setParams ("2");  
  81.             $request->setIdentityParam ($identity_param);  
  82.             //$request->setBizParams ( "{\"auth_code\":\"M_APPPC_CERT\",\"state\":\"透传参数\"}" ); //  
  83.             $url = $client->generatePageRedirectInvokeUrl ( $request );  
  84.             //dump($url);   
  85.             if($url){  
  86.                 $json['msg'] =1;   
  87.                 $json['url'] =$url;   
  88.             }else{  
  89.                 $json['msg'] =0;   
  90.                 $json['info'] ='参数错误';   
  91.             }  
  92.             echo json_encode($json);exit;  
  93.   
  94.               
  95.               
  96.         }else{  
  97.             $json['msg'] =0;   
  98.             $json['info'] ='参数错误';   
  99.             echo json_encode($json);exit;  
  100.         }  
  101.       
  102.       
  103.     }  
  104.       
  105.   
  106.     //返回  
  107.     public function returndata(){  
  108.         $params=$_GET['params'];  
  109.         $sign=$_GET['sign'];  
  110.         if(!$sign){  
  111.             $this->redirect('Member/rz');exit;  
  112.         }  
  113.         // 判断串中是否有%,有则需要decode  
  114.     //  dump($sign);  
  115.           
  116.         $params = strstr ( $params'%' ) ? urldecode ( $params ) : $params;  
  117.         $sign = strstr ( $sign'%' ) ? urldecode ( $sign ) : $sign;  
  118.           
  119.         $client = new ZmopClient ( $this->gatewayUrl, $this->app_id, $this->charset, $this->privateKeyFile, $this->zmPublicKeyFile );  
  120.         $result = $client->decryptAndVerifySign ( $params$sign );  
  121.         //转数组  
  122.         $parts = explode('&',$result);  
  123.         $array=array();  
  124.         foreach($parts as $k=>$v){  
  125.             $parts[$k] = explode('=',$v);  
  126.             $array[$parts[$k]['0']] = $parts[$k]['1'];  
  127.         }  
  128.         //dump($array['success']='false');  
  129.         if($array['success']!='false'){  
  130.               
  131.               
  132.             //dump($array);exit;  
  133.           
  134.             $res =$this->ToRz($array);  
  135.             if($res->success){  
  136.             //返回数据 更新会员信息  
  137.                 $is_zhima =  M('member')->where(array('id'=>cookie('id')))->find();  
  138.                 if($is_zhima['zhima']!=$res->zm_score){  
  139.                     $save['open_id'] = $array['open_id'];  
  140.                     $save['zhima'] = $res->zm_score;  
  141.                     $save['state'] = '1';  
  142.                     $save['optime'] = time();  
  143.                     $save['rztime'] = time();  
  144.                     $member = M('member')->where(array('id'=>cookie('id')))->save($save);  
  145.                       
  146.                 }  
  147.                 echo '<script>alert("认证成功");window.location.href ="/Member/rz"</script>';  
  148.             }  
  149.               
  150.             ///dump($member);  
  151.         //  dump($result);  
  152.         }else{  
  153.             //echo '<script>alert("验签失败");window.location.href ="/Member/rz"</script>';  
  154.             $this->redirect('Member/rz');exit;  
  155.         }  
  156.           
  157.           
  158.           
  159.     }  
  160.   
  161.   
  162.       
  163.   
  164.   
  165.   
  166.   
  167. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值