You are here

protected function OAuthRequester::curl_raw in Lingotek Translation 7.3

Same name and namespace in other branches
  1. 7.2 lib/oauth-php/library/OAuthRequester.php \OAuthRequester::curl_raw()

* Open and close a curl session passing all the options to the curl libs * *

Parameters

array opts the curl options.: * @exception OAuthException2 when temporary file for PUT operation could not be created * @return string the result of the curl action

1 call to OAuthRequester::curl_raw()
OAuthRequester::doRequest in lib/oauth-php/library/OAuthRequester.php
* Perform the request, returns the response code, headers and body. * *

File

lib/oauth-php/library/OAuthRequester.php, line 296

Class

OAuthRequester

Code

protected function curl_raw($opts = array()) {
  if (isset($opts[CURLOPT_HTTPHEADER])) {
    $header = $opts[CURLOPT_HTTPHEADER];
  }
  else {
    $header = array();
  }
  $ch = curl_init();
  $method = $this
    ->getMethod();
  $url = $this
    ->getRequestUrl();
  $header[] = $this
    ->getAuthorizationHeader();
  $query = $this
    ->getQueryString();
  $body = $this
    ->getBody();
  $has_content_type = false;
  foreach ($header as $h) {
    if (strncasecmp($h, 'Content-Type:', 13) == 0) {
      $has_content_type = true;
    }
  }
  if (!is_null($body)) {
    if ($method == 'TRACE') {
      throw new OAuthException2('A body can not be sent with a TRACE operation');
    }

    // PUT and POST allow a request body
    if (!empty($query)) {
      $url .= '?' . $query;
    }

    // Make sure that the content type of the request is ok
    if (!$has_content_type) {
      $header[] = 'Content-Type: application/octet-stream';
      $has_content_type = true;
    }

    // When PUTting, we need to use an intermediate file (because of the curl implementation)
    if ($method == 'PUT') {

      /*
      if (version_compare(phpversion(), '5.2.0') >= 0)
      {
      	// Use the data wrapper to create the file expected by the put method
      	$put_file = fopen('data://application/octet-stream;base64,'.base64_encode($body));
      }
      */
      $put_file = @tmpfile();
      if (!$put_file) {
        throw new OAuthException2('Could not create tmpfile for PUT operation');
      }
      fwrite($put_file, $body);
      fseek($put_file, 0);
      curl_setopt($ch, CURLOPT_PUT, true);
      curl_setopt($ch, CURLOPT_INFILE, $put_file);
      curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
    }
    else {
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    }
  }
  else {

    // a 'normal' request, no body to be send
    if ($method == 'POST') {
      if (!$has_content_type) {
        $header[] = 'Content-Type: application/x-www-form-urlencoded';
        $has_content_type = true;
      }
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    }
    else {
      if (!empty($query)) {
        $url .= '?' . $query;
      }
      if ($method != 'GET') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
      }
    }
  }
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_USERAGENT, 'anyMeta/OAuth 1.0 - ($LastChangedRevision: 174 $)');
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT, 90);
  foreach ($opts as $k => $v) {
    if ($k != CURLOPT_HTTPHEADER) {
      curl_setopt($ch, $k, $v);
    }
  }
  $txt = curl_exec($ch);
  if ($txt === false) {
    $error = curl_error($ch);
    curl_close($ch);
    throw new OAuthException2('CURL error: ' . $error);
  }
  curl_close($ch);
  if (!empty($put_file)) {
    fclose($put_file);
  }

  // Tell the logger what we requested and what we received back
  $data = $method . " {$url}\n" . implode("\n", $header);
  if (is_string($body)) {
    $data .= "\n\n" . $body;
  }
  else {
    if ($method == 'POST') {
      $data .= "\n\n" . $query;
    }
  }
  OAuthRequestLogger::setSent($data, $body);
  OAuthRequestLogger::setReceived($txt);
  return $txt;
}