<?php//私钥和公钥在芝麻信用后台设置,官网有详细说明classZhimaActionextendsCommonAction{//芝麻信用网关地址public$gatewayUrl="https://zmopenapi.zmxy.com.cn/openapi.do";//商户私钥文件public$privateKeyFile="商户私钥文件(绝对路径)";//芝麻公钥文件public$zmPublicKeyFile="芝麻公钥文件(绝对路径)";//数据编码格式public$charset="UTF-8";//应用idpublic$app_id="*******";//要调用的接口名public$method="zhima.credit.score.get";//来源平台,默认为zmoppublic$platform="zmop";//接口版本,目前只支持1.0public$version="1.0";//加密后信息RSA加密后的业务参数public$params="";//加密后信息对params参数加密前的签名,算法为SHA1WithRSApublic$sign="1.0";/*加签过程1、在加密过程的第一步,我们得到了拼接在一起的业务参数,同样以芝麻信用评分为例,拼接的参数如下:transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496)2、使用SHA1WithRSA算法以及商户自己的私钥进行签名,得到byte数组SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496))3、将byte数组进行Base64编码,得到一个签名的字符串Base64(SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496)))经过了上述三步,我们便得到了业务参数的签名,最后我们将这个签名的值放入系统参数sign中:sign=Base64(SHA1WithRSA(transaction_id=URLEncode(1234567)&product_code=URLEncode(w1010100100000000001)&open_id=URLEncode(268810000007909449496)))解密和验签*/publicfunction__construct(){parent::__construct();//在官网下载Vendor('zhima.zmop.ZmopClient');Vendor('zhima.zmop.RSAUtil');Vendor('zhima.zmop.ZhimaCreditIvsDetailGetRequest');Vendor('zhima.ZmopSdk');}publicfunctiongrant(){if(IS_POST){$name=I('name');//姓名$IDnumber=I('IDnumber');//×××号码$client=newZmopClient($this->gatewayUrl,$this->app_id,$this->charset,$this->privateKeyFile,$this->zmPublicKeyFile);$RSAUtil=newRSAUtil();$identity_type='2';$identity_param=json_encode(array('certNo'=>$IDnumber,'name'=>$name,'certType'=>"IDENTITY_CARD"));$request['app_id']=$this->app_id;$request['charset']=$this->charset;$request['method']='zhima.auth.info.authorize';$request['version']=$this->version;$request['platform']=$this->platform;$request['params']=$RSAUtil->rsaEncrypt($str,$this->zmPublicKeyFile);$request['sign']=$RSAUtil->sign($str,$this->privateKeyFile);$request['identity_type']=$identity_type;$request['identity_param']=$identity_param;$str='identity_type='.urlencode($identity_type).'&identity_param='.urlencode($identity_param).'';$request=newZhimaAuthInfoAuthorizeRequest();$request->setIdentityType("2");//$request->setParams("2");$request->setIdentityParam($identity_param);//$request->setBizParams("{\"auth_code\":\"M_APPPC_CERT\",\"state\":\"透传参数\"}");//$url=$client->generatePageRedirectInvokeUrl($request);//dump($url);if($url){$json['msg']=1;$json['url']=$url;}else{$json['msg']=0;$json['info']='参数错误';}echojson_encode($json);exit;}else{$json['msg']=0;$json['info']='参数错误';echojson_encode($json);exit;}}//返回publicfunctionreturndata(){$params=$_GET['params'];$sign=$_GET['sign'];if(!$sign){$this->redirect('Member/rz');exit;}//判断串中是否有%,有则需要decode//dump($sign);$params=strstr($params,'%')?urldecode($params):$params;$sign=strstr($sign,'%')?urldecode($sign):$sign;$client=newZmopClient($this->gatewayUrl,$this->app_id,$this->charset,$this->privateKeyFile,$this->zmPublicKeyFile);$result=$client->decryptAndVerifySign($params,$sign);//转数组$parts=explode('&',$result);$array=array();foreach($partsas$k=>$v){$parts[$k]=explode('=',$v);$array[$parts[$k]['0']]=$parts[$k]['1'];}//dump($array['success']='false');if($array['success']!='false'){//dump($array);exit;$res=$this->ToRz($array);if($res->success){//返回数据更新会员信息$is_zhima=M('member')->where(array('id'=>cookie('id')))->find();if($is_zhima['zhima']!=$res->zm_score){$save['open_id']=$array['open_id'];$save['zhima']=$res->zm_score;$save['state']='1';$save['optime']=time();$save['rztime']=time();$member=M('member')->where(array('id'=>cookie('id')))->save($save);}echo'<script>alert("认证成功");window.location.href="/Member/rz"</script>';}///dump($member);//dump($result);}else{//echo'<script>alert("验签失败");window.location.href="/Member/rz"</script>';$this->redirect('Member/rz');exit;}}}