itself in the micro-credit API class (that is, the so-called Advanced interface), we recommend that the cache process based on the actual environment, because the acquisition of Access_token Micro-letter official only 2000 times a day, get a valid time is 7,200 seconds, so we need to be in the effective time, Use the Access_token value repeatedly until the expiration is expired and then retrieved again! Below I will use the TP (thinkphp3.2.3) frame to write down:
First, use memcached (note because I under the TP version is the core version, please do the official Download memcached class library):
<?php
namespace Wx;
Class Wechatapi extends wechatjson{
Public Function cache ($key, $value = null, $timeout = 7000) {
$accessToken = S ($key, ', Array (' type ' => ' memcached '));
$expires _time = S ("{$key}_time", "," Array (' type ' => ' memcached '));
if (empty ($value)) {
if ($accessToken && $expires _time > Time ()) {
return $accessToken;
}
return false;
}
S ($key, $value, Array (' type ' => ' memcached '));
S ("{$key}_time", Time () + $timeout, Array (' type ' => ' memcached '));
return false;
}
}
Second, when using the database table:
Third, in the Sina SAE:
<?php
namespace Wx;
Class Wechatapi extends wechatjson{
Public Function cache ($key, $value = null, $timeout = 7000) {
$MMC = Memcache_init ();
$accessToken = Memcache_get ($mmc, $key);
$expires _time = Memcache_get ($mmc, $key. ' _time ');
if (empty ($value)) {
if ($accessToken && $expires _time > Time ()) {
return $accessToken;
}
return false;
}
Memcache_set ($mmc, $key, $value);
Memcache_set ($MMC, $key. _time ', time () + $timeout);
return false;
}
}
The test code shown at the beginning of the article:
<?php
namespace Api\controller;
Use Think\controller;
Use WX\WECHATAPI;
Use Wx\wechatrequest;
Class Indexcontroller extends Controller {
Public Function indexaction () {
$id = I (' Get.user ', ');
if ($id) {
$user = M (' config ')->where (Array (' UID ' => $id))->find ();
if ($user) {
$config = Json_decode ($user [' config ']);
$wechat = Wechatrequest::getinstance ($config);
$wechat->parse ();
}
}
}
Public Function testaction () {
$config = C (' wechat_conf ');
Echo ' Access_token: '. S ("{$config [' AppID ']}:access_token ', '", ' Array (' type ' => ' memcached ')). ' <br/> ';
Echo ' Expire_time: '. S ("{$config [' AppID ']}:access_token_time ', '", ' Array (' type ' => ' memcached ')). ' <br/> ';
$api = wechatapi::getinstance (Array (
wechatapi::app_id => $config [wechatapi::app_id],
Wechatapi::app_secret => $config [Wechatapi::app_secret]
));
$res = $api->call ('/menu/get ');
$res = $api->call ('/user/info ', Array (' OpenID ' => ' On0evjnystxkcsaacamycpmzdmwa ', ' lang ' => ' zh_cn '), Wechatapi::get);
if (! $res) {
Var_dump ($api->_error);
Var_dump ($api->_error_number);
}
Var_dump ($res);
}
}