Detailed description of the getAccessToken method and examples, and detailed description of getaccesstoken
Memcache caches and stores user information for 7000 seconds
<?phpfunction getAccessToken($appid,$appsecret) { $mem = new CacheMemcache(); $acc = $mem->get('access_token_'.$appid); if (!$acc) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $result = https_request($url); $jsoninfo = json_decode($result, true); $access_token = $jsoninfo['access_token']; if ($access_token) { $expire = time() + 7000; $mem = new CacheMemcache(); $mem->set('access_token_'.$appid,$access_token,$expire); } } else { $access_token = $acc; } return $access_token;}?>
File Storage access_token
Function getAccessToken () {// access_token should be stored and updated globally. The following code is written to the file for example $ data = json_decode (file_get_contents ("access_token.json ")); if ($ data-> expire_time <time () {$ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = $ this-> appId & secret = $ this-> appSecret "; $ res = json_decode ($ this-> httpGet ($ url )); $ access_token = $ res-> access_token; if ($ access_token) {$ data-> expire_time = time () + 7000; $ data-> access_token = $ access_token; $ fp = fopen ("access_token.json", "w"); fwrite ($ fp, json_encode ($ data); fclose ($ fp );}} else {$ access_token = $ data-> access_token;} return $ access_token ;}
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!