protected function _getTokenFromServer() { $params = ['appid' => $this->appId, 'secret' => $this->appSecret, 'grant_type' => 'client_credential']; $token = $this->parseJSON(Http::get(self::API_TOKEN_GET, $params)); if (empty($token['access_token'])) { trigger_error('Request AccessToken fail. response: ' . json_encode($token, JSON_UNESCAPED_UNICODE), E_USER_ERROR); } return $token; }
/** * 获取jsticket. * * @return string */ public function getTicket() { $key = 'pakey_wechat_jsapi_ticket.' . $this->appId; $data = Cache::get($key); if (!$data || empty($data['ticket'])) { $token = $this->getToken(); $data = $this->parseJSON(Http::get(self::API_TICKET, ['access_token' => $token])); if ($data && !empty($data['ticket'])) { Cache::set($key, $data, $data['expires_in'] / 10); } else { $data['ticket'] = null; } } return $data['ticket']; }
/** * 获取用户信息 * * @param string $openId * @param string $accessToken * * @return array */ public function getUser($openId, $accessToken) { $queries = array('access_token' => $accessToken, 'openid' => $openId, 'lang' => 'zh_CN'); return $this->parseJSON(Http::get(self::API_USER, $queries)); }
/** * 获取jsticket. * * @return string */ public function getTicket() { $key = 'pakey_wechat_jsapi_ticket.' . $this->appId; // for php 5.3 $appId = $this->appId; $appSecret = $this->appSecret; $cache = $this->cache; $apiTicket = self::API_TICKET; return $this->cache->get($key, function ($key) use($appId, $appSecret, $cache, $apiTicket) { $http = new Http(new AccessToken($appId, $appSecret)); $result = $http->get($apiTicket); $cache->set($key, $result['ticket'], $result['expires_in']); return $result['ticket']; }); }