WeChat get multiple get global access_token conflicts result in invalid? (sort of solved)

Source: Internet
Author: User

Token default is 7,200 seconds expired, if there is a second place to request the same token, then the first token will expire within 5 minutes. In other words, if the time is too short, the conflict will be caused by my second request.
Just learning to develop, the code is not very perfect writing. Only after the judgment is greater than 7200 lifetimes, see someone said can judge every minute read, do not know you greatly to my code what optimization suggestions? Thank you

function getWxAccessToken(){    if ( $_SESSION['access_token'] && $_SESSION['expire_time'] > time() ) {        //未过期        return $_SESSION['access_token'];    }else {        $appid        = "wx3bbf9ae731a6eb07";        $appsecret = "2b14a75fa48825ef3c76c8949b5e030f";        $url          = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;        $res =$this->https_request($url,'get','json');        $access_token = $res["access_token"];        //将重新获取到的access_token存到session里        $_SESSION['access_token']=$access_token;        $_SESSION['expire_time']=time()+7200;        return $access_token;     }}

Conflict is a method under the same controller, each call is

    $access_token=$this->getWxAccessToken();

The error message is as follows

array(2) {  ["errcode"] => int(40001)  ["errmsg"] => string(81) "invalid credential, access_token is invalid or not latest hint: [pd4MfA0688vr40!]"}

See what the authorities say.
0.0 The official meaning is not to use the business logic point to refresh token, I write as if it belongs to use business logic refresh, with the third party control what meaning?

The absolute path of the method is placed in the two-dimensional code generator, scanning. The result of such debugging is a sense of the correct point of access than the browser directly.
In most cases, the browser opens if you report 40001, basically the program is no problem

Reply content:

Token default is 7,200 seconds expired, if there is a second place to request the same token, then the first token will expire within 5 minutes. In other words, if the time is too short, the conflict will be caused by my second request.
Just learning to develop, the code is not very perfect writing. Only after the judgment is greater than 7200 lifetimes, see someone said can judge every minute read, do not know you greatly to my code what optimization suggestions? Thank you

function getWxAccessToken(){    if ( $_SESSION['access_token'] && $_SESSION['expire_time'] > time() ) {        //未过期        return $_SESSION['access_token'];    }else {        $appid        = "wx3bbf9ae731a6eb07";        $appsecret = "2b14a75fa48825ef3c76c8949b5e030f";        $url          = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;        $res =$this->https_request($url,'get','json');        $access_token = $res["access_token"];        //将重新获取到的access_token存到session里        $_SESSION['access_token']=$access_token;        $_SESSION['expire_time']=time()+7200;        return $access_token;     }}

Conflict is a method under the same controller, each call is

    $access_token=$this->getWxAccessToken();

The error message is as follows

array(2) {  ["errcode"] => int(40001)  ["errmsg"] => string(81) "invalid credential, access_token is invalid or not latest hint: [pd4MfA0688vr40!]"}

See what the authorities say.
0.0 The official meaning is not to use the business logic point to refresh token, I write as if it belongs to use business logic refresh, with the third party control what meaning?

The absolute path of the method is placed in the two-dimensional code generator, scanning. The result of such debugging is a sense of the correct point of access than the browser directly.
In most cases, the browser opens if you report 40001, basically the program is no problem

It is recommended to store Access_token in a database so that the same token is used throughout the business so that there is no case for each business logic point requesting access_token;
The official document means that if the business logic is on multiple servers, because Access_token and expire_time are stored in the session, the server will not share the session, which will cause the business on each server to initiate the request. If the access_token on one of the servers has not timed out yet another server has requested Access_token, the Access_token on the first server fails and then uses the expired Access_token to request the service, causing the service to become unstable
Use the central control server to store Access_token, so that unified service, no business conflicts between, ensure the consistency of data

Access_token to all the calls are generic, no need to put the session, everyone's session is not the same, generally put in the cache, or you put the database is good.
Every time used to go to the database inside the difference within the validity of the words, direct use, failure to regain, and then insert the database.

The session is user-based, so you can swipe once for each user's visit.
It is recommended to place the cache or database on a regular refresh.

  • 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.