use Think\Model;classDealJavaModel extends Model {/**
* @title 处理向java传参
* @param $url java接口地址
* @param $data 业务参数*/
publicfunction dealJavaParam($url,$data){//调java接口地址
$url=C('java_php').$url;
$token=C('token');
$apiKey=C('apiKey');
$timestamp=time();
$version=C('version');
$source=4;
$data=json_encode($data);//签名
$paramArr=array('token'=>$token,'timestamp'=>$timestamp,'source'=>$source,'version'=>$version,'data'=>$data
);
$sign=$this->createSign($paramArr,$apiKey);
$header= array('Content-Type: application/json; charset=utf-8',"token:$token","timestamp:$timestamp","source:$source","version:$version","sign:$sign","data:$data");
$result=$this->tocurl($url, $header,$data);
$res_info=json_decode($result,true);return$res_info;
}/**
* @title 签名函数
* @param $paramArr 系统参数
* @param $apiKey apikey
* @return string 返回签名*/
privatefunction createSign ($paramArr,$apiKey) {
ksort($paramArr);
$sign='';foreach ($paramArr as $key =>$val) {if ($key != '' && $val != '') {
$sign .= $key."=".$val."&";
}
}
$sign=rtrim($sign,"&");
$sign .=$apiKey;
$sign=strtolower($sign);
$sign=md5($sign);return$sign;
}/**
* @title 远程调java接口函数
* @param $url java接口地址
* @param $header 要传的头信息
* @param $data 业务参数*/
privatefunction tocurl($url, $header, $data){
$ch=curl_init();if(substr($url,0,5)=='https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); //跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); //从证书中检查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response=curl_exec($ch);if($error=curl_error($ch)){
die($error);
}
curl_close($ch);return$response;
}
}