You are here

public function WardenAPI::request in Warden 6

Same name and namespace in other branches
  1. 7 warden_api.inc \WardenAPI::request()

Send a message to warden

Parameters

string $path: The query path including the leading slash (e.g. '/public-key')

string $content: The body of the request. If this is not empty, the request is a post.

Return value

object The response object

Throws

Exception If the response status was not 200

2 calls to WardenAPI::request()
WardenAPI::getPublicKey in ./warden_api.inc
Get the public key.
WardenAPI::postSiteData in ./warden_api.inc
Send the site data to Warden.

File

./warden_api.inc, line 177
The API for communicating with the Warden server application.

Class

WardenAPI
@file The API for communicating with the Warden server application.

Code

public function request($path, $content = '') {
  $url = $this->wardenUrl . $path;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  if (!empty($this->username)) {
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Authorization: Basic ' . base64_encode($this->username . ':' . $this->password),
    ));
  }
  $warden_context_options = variable_get('warden_context_options', array());
  if (!empty($warden_context_options['ssl'])) {
    $verify_peer = isset($warden_context_options['ssl']['verify_peer']) ? $warden_context_options['ssl']['verify_peer'] : TRUE;
    $verify_host = isset($warden_context_options['ssl']['verify_host']) ? $warden_context_options['ssl']['verify_host'] : TRUE;
    $cafile = isset($warden_context_options['ssl']['cafile']) ? $warden_context_options['ssl']['cafile'] : FALSE;
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_peer);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify_host);
    if (!empty($cafile)) {
      curl_setopt($ch, CURLOPT_CAINFO, $cafile);
    }
  }
  if (!empty($content)) {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
  }
  $response = curl_exec($ch);
  $info = curl_getinfo($ch);
  $result = (object) $info;
  $result->data = $response;
  $result->code = $result->http_code;
  $result->error = curl_error($ch);
  curl_close($ch);
  if ($result->code != 200) {
    watchdog('warden', '@url : @code : @message', array(
      '@url' => $url,
      '@code' => $result->code,
      '@message' => $result->error,
    ), WATCHDOG_ERROR);
    throw new Exception('Unable to communicate with Warden');
  }
  return $result;
}