微信小程序 php解密获取手机号 + -41003错误

2020-07-20  本文已影响0人  莫奈的有趣灵魂
public function getInfo()
    {
        if (IS_POST) {
            $post = input('post.');
            require VENDOR_PATH . 'wechatgetphone/wxBizDataCrypt.php';
            $appInfo = $this->modelObj->getValue('minipro');
            //通过code获得 access_token + openid
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appInfo['appId']
                . "&secret=" . $appInfo['appSecret'] . "&js_code=" . $post['code'] . "&grant_type=authorization_code";

            $jsonResult = file_get_contents($url);
            if (!empty($jsonResult)) {
                $resultArray = json_decode($jsonResult, true);
                $post['encryptedData'] = urldecode($post['encryptedData']);
                $post['iv'] = urldecode($post['iv']);
                $pc      = new \WXBizDataCrypt($appInfo['appId'], $resultArray['session_key']);
                $errCode = $pc->decryptData($post['encryptedData'], $post['iv'], $data);

                if ($errCode == 0) {
                    exit($data);
                } else {
                    exit(json_encode($errCode));
                }
            }
        }
    }

此处需要注意的是,很多人解密的时候会报-41003
这是因为传过来的encryptedData的值有问题,
一定要在小程序端使用encodeURIComponent()进行数据编码后再传,
不然+号会变成空格,然后一直解密不成功......

上一篇 下一篇

猜你喜欢

热点阅读