WeChat public number to access user information, encountered invalid code problem

Source: Internet
Author: User
Tags openid
The php,php section is used as follows:

$code = $_get["code"];    $userinfo = GetUserInfo ($code);        function GetUserInfo ($code) {$appid = "1111111111";        $appsecret = "1111111111111";//appid and Appsecret are hidden here, in the source code is correct $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= $open        ID ";        $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; }

And now the problem is that there was a problem with the Userinfo_json, which caused the final result to fail to show.
Intermediate process to Userinfo_json output, prompt {"Errcode": 40029, "errmsg": "Invalid Code"}

And if I copy the Userinfo_url link on my phone, I can still call the user's details.

So what's the problem? What's the answer? Ask the Great God for answers

Reply content:

The php,php section is used as follows:

$code = $_get["code"];    $userinfo = GetUserInfo ($code);        function GetUserInfo ($code) {$appid = "1111111111";        $appsecret = "1111111111111";//appid and Appsecret are hidden here, in the source code is correct $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= $open        ID ";        $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; }

And now the problem is that there was a problem with the Userinfo_json, which caused the final result to fail to show.
Intermediate process to Userinfo_json output, prompt {"Errcode": 40029, "errmsg": "Invalid Code"}

And if I copy the Userinfo_url link on my phone, I can still call the user's details.

So what's the problem? What's the answer? Ask the Great God for answers

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

Take a good look at the documentation, the question is a bit big.

You are wrong in this case!
$userinfo _json = https_request ($access _token_url);
Should be
$userinfo _json = https_request ($userinfo _url);

This causes code to be used two times, code is allowed only once, valid for 5 minutes

Again:code can only be used once and cannot be refreshed

Https://github.com/thenbsp/wechat

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.