callmez\wechat\sdk\Wechat::getAccessToken PHP Method

getAccessToken() public method

获取AccessToken 会自动判断超时时间然后重新获取新的token (会智能缓存accessToken)
public getAccessToken ( boolean $force = false ) : string
$force boolean 是否强制获取
return string
    public function getAccessToken($force = false)
    {
        if ($this->_accessToken === null || $this->_accessToken['expire'] < YII_BEGIN_TIME || $force) {
            $result = !$force && $this->_accessToken === null ? $this->getCache('access_token', false) : false;
            if ($result === false) {
                if (!($result = $this->requestAccessToken())) {
                    throw new HttpException(500, 'Fail to get access_token from wechat server.');
                }
                $this->trigger(self::EVENT_AFTER_ACCESS_TOKEN_UPDATE, new Event(['data' => $result]));
                $this->setCache('access_token', $result, $result['expires_in']);
            }
            $this->setAccessToken($result);
        }
        return $this->_accessToken['token'];
    }
Wechat