/** * Make an api request. * * @param string $url * @param string $httpMethod The http method to use with this request * @param string $data Optional data to send with request * @param string $contentType * @return array The json parsed response from the server */ private function api($url, $httpMethod = 'GET', $data = null, $contentType = null) { // trigger an event $this->trigger('api_request_init', array('url' => $url)); $this->authenticate(); $accessToken = $this->auth->getAccessToken(); if (!$accessToken) { // should have a token at this point // if not, something went wrong throw new \Exception('Cannot make an api request without an access token'); } // save this request in case we need to use the refresh token $lastRequest = array('url' => $url, 'method' => $httpMethod, 'data' => $data); // add the access token onto the url $url = $url . '?access_token=' . $accessToken; $this->trigger('api_request_send', array('url' => $url, 'http_method' => $httpMethod, 'data' => $data)); $this->request->setMethod($httpMethod)->setData($data); if ($contentType) { $this->request->setContentType("application/{$contentType}"); } // now send the request $this->request->send($url); $this->trigger('api_request_complete', array('url' => $url, 'response' => $this->request->getResponse())); // check the response for any errors $vaild = $this->checkResponse(); if (!$vaild && $this->isTokenExpired) { // blow out the current token so a new one gets requested $this->auth->clearAccessToken(); // redo the last api request $this->api($lastRequest['url'], $lastRequest['method'], $lastRequest['data']); } return $this->request->getResponse(); }