PHP 蚂蚁芝麻信用分接口

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

你可能感兴趣的:(PHP 蚂蚁芝麻信用分接口)