当前位置:Gxlcms > PHP教程 > 微信公众号调取用户信息,遇到invalidcode的问题

微信公众号调取用户信息,遇到invalidcode的问题

时间:2021-07-01 10:21:17 帮助过:83人阅读

使用了php,php部分如下:

$code = $_GET["code"];


    $userinfo = getUserInfo($code);

    function getUserInfo($code)
    {
        $appid = "1111111111";
        $appsecret = "1111111111111";//appid和appsecret在这里隐去,在源代码中是正确的
        $access_token = "";

        //Get access_token
        $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
        $access_token_json = https_request($access_token_url);
        $access_token_array = json_decode($access_token_json,true);
        $access_token = $access_token_array['access_token'];
        $openid = $access_token_array['openid'];

        //Get user info
        $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid";
        $userinfo_json = https_request($access_token_url);
        $userinfo_array = json_decode($userinfo_json,ture);
        echo $userinfo_json;

        return userinfo_array;
    }

    function https_request($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl,  CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)){
            return 'ERROR'.curl_error($curl);
        }
        curl_close($curl);
        return $data;
    }

然后现在出现的问题就是,调userinfo_json时出现了问题,导致了最后结果无法显示。
中间过程把userinfo_json输出时,提示{"errcode":40029,"errmsg":"invalid code"}

而如果我把userinfo_url的链接复制下来在手机打开的话,还是能调用到用户的详细资料

所以问题到底出现在了哪里?有什么解决办法?求大神解答

回复内容:

使用了php,php部分如下:

$code = $_GET["code"];


    $userinfo = getUserInfo($code);

    function getUserInfo($code)
    {
        $appid = "1111111111";
        $appsecret = "1111111111111";//appid和appsecret在这里隐去,在源代码中是正确的
        $access_token = "";

        //Get access_token
        $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
        $access_token_json = https_request($access_token_url);
        $access_token_array = json_decode($access_token_json,true);
        $access_token = $access_token_array['access_token'];
        $openid = $access_token_array['openid'];

        //Get user info
        $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid";
        $userinfo_json = https_request($access_token_url);
        $userinfo_array = json_decode($userinfo_json,ture);
        echo $userinfo_json;

        return userinfo_array;
    }

    function https_request($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl,  CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)){
            return 'ERROR'.curl_error($curl);
        }
        curl_close($curl);
        return $data;
    }

然后现在出现的问题就是,调userinfo_json时出现了问题,导致了最后结果无法显示。
中间过程把userinfo_json输出时,提示{"errcode":40029,"errmsg":"invalid code"}

而如果我把userinfo_url的链接复制下来在手机打开的话,还是能调用到用户的详细资料

所以问题到底出现在了哪里?有什么解决办法?求大神解答

http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

先好好看看文档吧,这个问题有点大。。

你的这里是写错了吧!
$userinfo_json = https_request($access_token_url);
应该是
$userinfo_json = https_request($userinfo_url);

这样导致code用了两次,code只允许用一次,5分钟内有效

再说一次:code 只能使用一次,不能刷新

https://github.com/thenbsp/wechat

人气教程排行