You are here

protected function ServicesClientDrupalOAuthClient::get in Services Client 7

Same name and namespace in other branches
  1. 7.2 services_client_connection/modules/services_client_oauth/plugins/ServicesClientDrupalOAuthClient.inc \ServicesClientDrupalOAuthClient::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.
  • 'force_port' A port number that is forced when creating normalized URL for calculating signature.

Return value

string a string containing the response body.

2 calls to ServicesClientDrupalOAuthClient::get()
ServicesClientDrupalOAuthClient::getAccessToken in services_client_connection/modules/services_client_oauth/plugins/ServicesClientDrupalOAuthClient.inc
Fetches the access token using the request token.
ServicesClientDrupalOAuthClient::getRequestToken in services_client_connection/modules/services_client_oauth/plugins/ServicesClientDrupalOAuthClient.inc
Gets a request token from the provider.

File

services_client_connection/modules/services_client_oauth/plugins/ServicesClientDrupalOAuthClient.inc, line 196
Custom DrupalOAuthRequest implementation

Class

ServicesClientDrupalOAuthClient
@file Custom DrupalOAuthRequest implementation

Code

protected function get($path, $options = array()) {
  $options += array(
    'token' => FALSE,
    'params' => array(),
    'realm' => NULL,
    'get' => FALSE,
    'force_port' => NULL,
  );
  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 = ServicesClientOAuthRequest::from_consumer_and_token($this->consumer, $token, $options['get'] ? 'GET' : 'POST', $path, $options['params']);
  if ($options['force_port']) {
    $req->force_port = $options['force_port'];
  }
  $req
    ->sign_request($this->signatureMethod, $this->consumer, $token);
  $url = $req
    ->get_normalized_http_url(FALSE);
  $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);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $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;
}