微信小程序获取用户手机号,服务器解码demo

原理:通过微信登陆接口wx.login得到encryptedData 、 iv  、code.经过接口处理code得到sessionkey。最后官方demo得到解密后的手机号。(接口处理这一步也可以在小程序里操作wx.request,不必在后台处理。)

demo.php源码:

<?php include_once "wxBizDataCrypt.php"; $appid = \'wxda4661c645a843fd\';$appsecret=\'\'; //必须接收参数:encryptedData iv code $encryptedData=$_POST[\'encryptedData\']; $JSCODE=$_POST[\'code\'];$iv=$_POST[\'iv\']; //使用code转化得到sessionkey $url=\'https://api.weixin.qq.com/sns/jscode2session?appid=\'.$appid.\'&secret=\'.$appsecret.\'&js_code=\'.$JSCODE.\'&grant_type=authorization_code\'; $res=json_decode(file_get_contents($url,1)); $sessionKey=$res[\'sessionKey\']; //解码得到手机号 $pc = new WXBizDataCrypt($appid, $sessionKey); $errCode = $pc->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) { print($data . "\n"); } else { print($errCode . "\n"); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgwzwj.html