美文网首页
微信小程序 php解密获取手机号 + -41003错误

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

作者: 莫奈的有趣灵魂 | 来源:发表于2020-07-20 14:29 被阅读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()进行数据编码后再传,
    不然+号会变成空格,然后一直解密不成功......

    相关文章

      网友评论

          本文标题:微信小程序 php解密获取手机号 + -41003错误

          本文链接:https://www.haomeiwen.com/subject/mnzekktx.html