/** * Perform an OAuth request - these differ somewhat from regular API calls * @internal */ public function oauth_exchange($endpoint, array $args) { // build a post request and authenticate via OAuth header $params = new TwitterOAuthParams($args); $params->set_consumer($this->Consumer); if ($this->AccessToken) { $params->set_token($this->AccessToken); } $params->sign_hmac('POST', $endpoint); $conf = array('method' => 'POST', 'redirection' => 0, 'headers' => array('Authorization' => $params->oauth_header())); $http = self::http_request($endpoint, $conf); $body = trim($http['body']); $stat = $http['response']['code']; if (200 !== $stat) { throw new TwitterApiException($body, -1, $stat); } parse_str($body, $params); if (!is_array($params) || !isset($params['oauth_token']) || !isset($params['oauth_token_secret'])) { throw new TwitterApiException(__('Malformed response from Twitter', 'twitter-api'), -1, $stat); } return $params; }
/** * Sign and execute REST API call * @return array */ private function rest_request($path, array $args, $http_method) { // all calls must be authenticated in API 1.1 if (!$this->has_auth()) { throw new TwitterApiException('Twitter client not authenticated', 0, 401); } // prepare HTTP request config $conf = array('method' => $http_method); // build signed URL and request parameters $endpoint = TWITTER_API_BASE . '/' . $path . '.json'; $params = new TwitterOAuthParams($args); $params->set_consumer($this->Consumer); $params->set_token($this->AccessToken); $params->sign_hmac($http_method, $endpoint); if ('GET' === $http_method) { $endpoint .= '?' . $params->serialize(); } else { $conf['body'] = $params->serialize(); } $http = self::http_request($endpoint, $conf); // remember current rate limits for this endpoint $this->last_call = $path; if (isset($http['headers']['x-rate-limit-limit'])) { $this->last_rate[$path] = array('limit' => (int) $http['headers']['x-rate-limit-limit'], 'remaining' => (int) $http['headers']['x-rate-limit-remaining'], 'reset' => (int) $http['headers']['x-rate-limit-reset']); } return $http; }