解密我记得要微信文档里:微信加密说明
这里用Thinkphp3.2例子
$iv = define_str_replace(I('get.iv')); //把空格转成+
$encryptedData = urldecode(I('get.encryptedData')); //解码
$code = define_str_replace(I('get.code')); //把空格转成+
$msg = $this->getUserInfo($code,$encryptedData,$iv); //获取微信用户信息(openid)
public function getUserInfo($code,$encryptedData,$iv)
{
$appid = '你的appid';
$secret = '你的密钥';
$grant_type='authorization_code';
$url='https://api.weixin.qq.com/sns/jscode2session';
$url= sprintf("%s?appid=%s&secret=%s&js_code=%s&grant_type=%",$url,$appid,$secret,$code,$grant_type);
$user_data=json_decode(file_get_contents($url));
$session_key= define_str_replace($user_data->session_key);
$data="";
vendor('Vendor.WxApp.wxBizDataCrypt',realpath('./'),'.php');//引入微信解密文件
$wxBizDataCrypt = new \WXBizDataCrypt($appid,$session_key);
$errCode=$wxBizDataCrypt->decryptData($encryptedData,$iv,$data);
return ['errCode'=>$errCode,'data'=>json_decode($data),'session_key'=>$session_key];
}
/**
* 请求过程中因为编码原因+号变成了空格
* 需要用下面的方法转换回来
*/
function define_str_replace($data)
{
return str_replace(' ','+',$data);
}