You are here

protected function DrupalOAuth2Client::get in OAuth Connector 7

1 call to DrupalOAuth2Client::get()
DrupalOAuth2Client::getAccessToken in modules/oauth2/lib/DrupalOAuth2Client.inc

File

modules/oauth2/lib/DrupalOAuth2Client.inc, line 86

Class

DrupalOAuth2Client

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) && $options['get']) {
    $url .= '?' . drupal_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, drupal_http_build_query($params));

    // required for Google+?
  }
  $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;
}