You are here

protected function DrupalOAuthClient::get in OAuth 1.0 7.3

Same name and namespace in other branches
  1. 6.3 includes/DrupalOAuthClient.inc \DrupalOAuthClient::get()
  2. 7.4 includes/DrupalOAuthClient.inc \DrupalOAuthClient::get()

Make an OAuth request.

Parameters

string $path: The path being requested.

  • If you provide the full URL (e.g. "http://example.com/oauth/request_token"), then it will be used.
  • If you provide only the path (e.g. "oauth/request_token"), it will be converted into a full URL by prepending the provider_url.

array $options: An associative array of additional options, with the following keys:

  • 'token' (default FALSE) Whether a token should be used or not.
  • 'params' An associative array of parameters that should be included in the request.
  • 'realm' A string to be used as the http authentication realm in the request.
  • 'get' (default FALSE) Whether to use GET as the HTTP-method instead of POST.

Return value

string a string containing the response body.

2 calls to DrupalOAuthClient::get()
DrupalOAuthClient::getAccessToken in includes/DrupalOAuthClient.inc
Fetches the access token using the request token.
DrupalOAuthClient::getRequestToken in includes/DrupalOAuthClient.inc
Gets a request token from the provider.

File

includes/DrupalOAuthClient.inc, line 290

Class

DrupalOAuthClient

Code

protected function get($path, $options = array()) {
  $options += array(
    'token' => FALSE,
    'params' => array(),
    'realm' => NULL,
    'get' => FALSE,
  );
  if (empty($options['realm']) && !empty($this->consumer->configuration['authentication_realm'])) {
    $options['realm'] = $this->consumer->configuration['authentication_realm'];
  }
  $token = $options['token'] ? $this->requestToken : NULL;
  $path = $this
    ->getAbsolutePath($path);
  $req = OAuthRequest::from_consumer_and_token($this->consumer, $token, $options['get'] ? 'GET' : 'POST', $path, $options['params']);
  $req
    ->sign_request($this->signatureMethod, $this->consumer, $token);
  $url = $req
    ->get_normalized_http_url();
  $params = array();
  foreach ($req
    ->get_parameters() as $param_key => $param_value) {
    if (substr($param_key, 0, 5) != 'oauth') {
      $params[$param_key] = $param_value;
    }
  }
  if (!empty($params)) {
    $url .= '?' . http_build_query($params);
  }
  $headers = array(
    'Accept: application/x-www-form-urlencoded',
    $req
      ->to_header($options['realm']),
  );
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  if (!$options['get']) {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, '');
  }
  $oauth_version = _oauth_common_version();
  curl_setopt($ch, CURLOPT_USERAGENT, 'Drupal/' . VERSION . ' OAuth/' . $oauth_version);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $response = curl_exec($ch);
  $error = curl_error($ch);
  curl_close($ch);
  if ($error) {
    throw new Exception($error);
  }
  $result = $this
    ->interpretResponse($response);
  if ($result->responseCode != 200) {
    throw new Exception('Failed to fetch data from url "' . $path . '" (HTTP response code ' . $result->responseCode . ' ' . $result->responseMessage . '): ' . $result->body, $result->responseCode);
  }
  return $result->body;
}