You are here

public function Github_HttpClient_Curl::doRequest in Bibliography Module 7.2

Send a request to the server, receive a response

Parameters

string $path Request url:

array $parameters Parameters:

string $httpMethod HTTP method to use:

array $options Request options:

Return value

string HTTP response

Overrides Github_HttpClient::doRequest

File

modules/CiteProc/Github/HttpClient/Curl.php, line 21

Class

Github_HttpClient_Curl
Performs requests on GitHub API. API documentation should be self-explanatory.

Code

public function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options) {
  $curlOptions = array();
  if ($options['login']) {
    switch ($options['auth_method']) {
      case Github_Client::AUTH_HTTP_PASSWORD:
        $curlOptions += array(
          CURLOPT_USERPWD => $options['login'] . ':' . $options['secret'],
        );
        break;
      case Github_Client::AUTH_HTTP_TOKEN:
        $curlOptions += array(
          CURLOPT_USERPWD => $options['login'] . '/token:' . $options['secret'],
        );
        break;
      case Github_Client::AUTH_URL_TOKEN:
      default:
        $parameters = array_merge(array(
          'login' => $options['login'],
          'token' => $options['secret'],
        ), $parameters);
        break;
    }
  }
  if (!empty($parameters)) {
    $queryString = utf8_encode(http_build_query($parameters, '', '&'));
    if ('GET' === $httpMethod) {
      $url .= '?' . $queryString;
    }
    else {
      $curlOptions += array(
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $queryString,
      );
    }
  }
  $curlOptions += array(
    CURLOPT_URL => $url,
    CURLOPT_PORT => $options['http_port'],
    CURLOPT_USERAGENT => $options['user_agent'],
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => $options['timeout'],
  );
  $response = $this
    ->doCurlCall($curlOptions);
  if (!in_array($response['headers']['http_code'], array(
    0,
    200,
    201,
  ))) {
    throw new Github_HttpClient_Exception(null, (int) $response['headers']['http_code']);
  }
  if ($response['errorNumber'] != '') {
    throw new Github_HttpClient_Exception('error ' . $response['errorNumber']);
  }
  return $response['response'];
}