编辑: Mckel0ve | 2019-07-09 |
digest.update(str.getBytes());
// 获取字节数组 byte messageDigest[] = digest.digest();
// 字节数组转换为 十六进制 数StringBuffer hexString = new StringBuffer();
for (int i = 0;
i < messageDigest.length;
i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) { hexString.append(0);
} hexString.append(shaHex);
} String msg_signature = hexString.toString().toLowerCase();
System.out.println(msg_signature);
} catch (Exception e) { e.printStackTrace();
} } } 例3(PHP): $nonce=rand(100000,999999);
$array=array( 'app_id'=>'wx5ac980159ad6ff86', 'open_id'=>$res['FromUserName'], 'nick_name'=>$res['FromUserName'], 'msgType'=>$res['MsgType'], 'content'=>$res['Content'], 'timestamp'=>$res['CreateTime'], 'nonce'=>intval($nonce), );
$mix_arr=array( 'open_id'=>$res['FromUserName'], 'timestamp'=>$res['CreateTime'], 'nonce'=>intval($nonce), 'token'=>'c2aed800324911e8', );
$url="http://wechat.7moor.com/Icallback";
$mix_arrs=array_combine($mix_arr,$mix_arr);
ksort($mix_arrs,2);
foreach($mix_arrs as $val) { $sign_string.=$val;
} eblog("签名串",$sign_string,'test_'.date("Ymd"),'test/');
//$new_sign = strtoupper(sha1($sign_string));
$new_sign = sha1($sign_string);
$array['msg_signature']=$new_sign;
$json_str=json_encode($array,JSON_UNESCAPED_UNICODE);
eblog("请求报文",$json_str,'test_'.date("Ymd"),'test/');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, true);
//类型为json curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json;
charset=utf-8', 'Content-Length:' . strlen($json_str) ) );
//post传递 curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, ($json_str));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$ress = curl_exec($curl);
$errorno = curl_errno($curl);
curl_close($curl);