WeChat public platform development automatically updates WeChat accesstoken

Source: Internet
Author: User
Access_token is the globally unique ticket of the public account. access_token is required when the public account calls each interface. Under normal circumstances, the validity period of the access_token is 7200 seconds. repeated access_token acquisition will invalidate the previous access_token. This topic describes how to store and update access tokens.

1. Access Token

Access_token is the globally unique ticket of the public account. access_token is required when the public account calls each interface. Under normal circumstancesAccess_token is valid for 7200 seconds, Duplicate access will invalidate the last access_token.

The public account can use AppID and AppSecret to call this interface to obtain the access_token. AppID and AppSecret can be obtained in the development mode (you need to become a developer and the account is not abnormal ).Note that https is required to call all interfaces.

API call request description

Http request method: GEThttps: // api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = APPID & secret = APPSECRET

Parameter description

Parameters Required? Description
Grant_type Yes Obtain access_token and enter client_credential
Appid Yes Unique third-party user credential
Secret Yes The unique credential key of a third-party user, which is appsecret

Return description

Normally, the following JSON data packet is returned to the public account:

{"access_token":"ACCESS_TOKEN","expires_in":7200}

III. implementation

Class class_weixin {var $ appid = APPID; var $ appsecret = APPSECRET; // Constructor to obtain Access Token public function _ construct ($ appid = NULL, $ appsecret = NULL) {if ($ appid & $ appsecret) {$ this-> appid = $ appid; $ this-> appsecret = $ appsecret;} // 1. database format/* drop table if exists 'wx _ token'; create table if not exists 'wx _ token' ('id' int (1) not null, 'type' varchar (20) not null, 'expire 'varchar (16) not null, 'value' varchar (600) not null, primary key ('id ')) ENGINE = MyISAM default charset = utf8; insert into 'wx _ token' ('id', 'type', 'expire ', 'value') VALUES (1, 'access _ token', '000000', 't3oyw9frnowkqhqhzxoeh-pgThhjmnCqTVpaLyUD '), (2, 'jsapi _ ticket ','',''); */$ con = mysql_connect (MYSQLHOST. ':'. MYSQLPORT, MYSQLUSER, MYSQLPASSWORD); mysql_select_db (MYSQLDATABASE, $ con ); $ result = mysql_query ("SELECT * FROM 'wx _ token' WHERE 'type' = 'Access _ token'"); while ($ row = mysql_fetch_array ($ result )) {$ this-> access_token = $ row ['value']; $ this-> expires_time = $ row ['expire ']; break;} if (time ()> ($ this-> expires_time + 3600) {$ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= ". $ This-> appid. "& secret = ". $ this-> appsecret; $ res = $ this-> http_request ($ url); $ result = json_decode ($ res, true ); $ this-> access_token = $ result ["access_token"]; $ this-> expires_time = time (); mysql_query ("UPDATE 'wx _ token' SET 'expire '=' $ this-> expires_time ', 'value' = '$ this-> access_token 'Where 'type' = 'Access _ token'; ");} // 2. cache format if (isset ($ _ SERVER ['http _ appname']) {// in the SAE environment, activate memcache $ mem = Memcache_init ();} else {// local environment. you must have installed memcache $ mem = new Memcache; $ mem-> connect ('localhost', 11211) or die ("cocould not connect");} $ this-> access_token = $ mem-> get ($ this-> appid); if (! Isset ($ this-> access_token) | empty ($ this-> access_token) {$ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= ". $ This-> appid. "& secret = ". $ this-> appsecret; $ res = $ this-> http_request ($ url); $ result = json_decode ($ res, true ); $ this-> access_token = $ result ["access_token"]; $ mem-> set ($ this-> appid, $ this-> access_token, 0, 3600 );} // 3. locally write $ res = file_get_contents ('Access _ token. json '); $ result = json_decode ($ res, true); $ this-> expires_time = $ result ["expires_time"]; $ this-> access_token = $ result ["access_token"]; if (time ()> ($ this-> expires_time + 3600) {$ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= ". $ This-> appid. "& secret = ". $ this-> appsecret; $ res = $ this-> http_request ($ url); $ result = json_decode ($ res, true ); $ this-> access_token = $ result ["access_token"]; $ this-> expires_time = time (); file_put_contents ('Access _ token. json ',' {"access_token ":"'. $ this-> access_token. '"," expires_time ":'. $ this-> expires_time. '}');} // 4. real-time pulling $ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= ". $ This-> appid. "& secret = ". $ this-> appsecret; $ res = $ this-> http_request ($ url); $ result = json_decode ($ res, true ); $ this-> access_token = $ result ["access_token"]; $ this-> expires_time = time ();}

For more articles related to the automatic update of access token for public platform development, please follow the PHP Chinese network!

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.