/** * @return Response */ protected function getResponse() { $responseHeaders = $this->oAuthClient->getLastResponseHeaders(); $response = Response::createFromRaw($responseHeaders); $response->appendContent($this->oAuthClient->getLastResponse()); return $response; }
protected static function fetch_tarball_via_oauth($key, $secret, $uri) { try { $oauth = new OAuth($key, $secret); $oauth->fetch($uri); WP_CLI::debug($oauth->getLastResponseHeaders()); $headers = http_parse_headers($oauth->getLastResponseHeaders()); $mime_type = self::parse_content_type_header($headers['Content-Type']); $content_disposition = self::parse_content_disposition_header($headers['Content-Disposition']); $filename = empty($content_disposition['filename']) ? $filename = tmpfile() : sys_get_temp_dir() . DIRECTORY_SEPARATOR . $content_disposition['filename']; file_put_contents($filename, $oauth->getLastResponse()); return $filename; } catch (OAuthException $e) { WP_CLI::error_multi_line($e->getMessage(), true); } WP_CLI::error('Unknown error', true); }
/** * fetches JSON request on F1, parses and returns response * @param string $url * @param string|array $data * @param const $method * @return void */ public function fetchJson($url, $data = null, $method = OAUTH_HTTP_METHOD_GET) { try { $o = new OAuth($this->settings->key, $this->settings->secret, OAUTH_SIG_METHOD_HMACSHA1); $o->setToken($this->accessToken->oauth_token, $this->accessToken->oauth_token_secret); $headers = array('Content-Type' => 'application/json'); if ($o->fetch($url, $data, $method, $headers)) { if ($this->settings->debug) { return array('headers' => self::http_parse_headers($o->getLastResponseHeaders()), 'response' => json_decode($o->getLastResponse(), true)); } return json_decode($o->getLastResponse(), true); } } catch (OAuthException $e) { $this->error = array('method' => $method, 'url' => $url, 'response' => self::http_parse_headers($o->getLastResponseHeaders())); return $this->error; } }
/** * Returns the response headers and response code from a called OAuth object * * @param OAuth $oauth A called OAuth object * @return array elements are 0: HTTP response code; 1: response content, 2: HTTP response headers */ private function GetOAuthResponseHeaders($oauth) { $response_code = NULL; $response_xml = NULL; $response_headers = array(); try { $response_xml = $oauth->getLastResponse(); $response_headers = array(); $response_headers_raw = $oauth->getLastResponseHeaders(); $response_headers_rows = explode("\r\n", $response_headers_raw); foreach ($response_headers_rows as $header) { $keyval = explode(":", $header); if (2 == count($keyval)) { $response_headers[$keyval[0]] = trim($keyval[1]); } if (FALSE !== strpos($header, 'HTTP')) { list(, $response_code, ) = explode(' ', $header); } } // Decompress, if applicable if ('QBO' == $this->context->serviceType || 'QBD' == $this->context->serviceType) { // Even if accept-encoding is set to deflate, server never (as far as we know) actually chooses // to respond with Content-Encoding: deflate. Thus, the inspection of 'Content-Encoding' response // header rather than assuming that server will respond with encoding specified by accept-encoding if ($this->ResponseCompressor && $response_headers && array_key_exists('Content-Encoding', $response_headers)) { $response_xml = $this->ResponseCompressor->Decompress($response_xml, $response_headers); } } } catch (Exception $e) { } return array($response_code, $response_xml, $response_headers); }
/** * obtain credentials based access token from API * @param string $username * @param string $password * @param boolean $returnHeaders=false * @return array */ protected function obtainCredentialsBasedAccessToken($username, $password, $returnHeaders = false) { try { $message = urlencode(base64_encode("{$username} {$password}")); $url = $this->settings->baseUrl . self::$paths['portalUser']['accessToken'] . "?ec={$message}"; $o = new OAuth($this->settings->key, $this->settings->secret, OAUTH_SIG_METHOD_HMACSHA1); $token = $o->getAccessToken($url); if ($returnHeaders) { $token['headers'] = self::http_parse_headers($o->getLastResponseHeaders()); } return (object) $token; } catch (OAuthException $e) { $this->error = array('error' => true, 'code' => $e->getCode(), 'response' => $e->lastResponse, 'url' => $url); return false; } }