You are here

public function SimpleRestMessage::send in Acquia Cloud Site Factory Connector 8.2

Same name in this branch
  1. 8.2 acsf_init/lib/sites/g/SimpleRest.php \Acquia\SimpleRest\SimpleRestMessage::send()
  2. 8.2 acsf_init/lib/cloud_hooks/common/pre-web-activate/000-acquia-deployment.php \SimpleRestMessage::send()
Same name and namespace in other branches
  1. 8 acsf_init/lib/cloud_hooks/common/pre-web-activate/000-acquia-deployment.php \SimpleRestMessage::send()

Sends a request.

Parameters

string $method: The request method. Either 'POST' or 'GET'.

string $endpoint: The request endpoint.

array $parameters: Any required parameters for the request. Note: parameters are currently only implemented for POST requests. To add support for GET parameters would require changes in this method.

SimpleRestCreds $creds: The credentials to use for the Site Factory request.

Return value

\SimpleRestResponse The response.

Throws

Exception If the request fails.

File

acsf_init/lib/cloud_hooks/common/pre-web-activate/000-acquia-deployment.php, line 388
This script is responsible for deploying theme files on each webnode.

Class

SimpleRestMessage
Class SimpleRestMessage.

Code

public function send($method, $endpoint, array $parameters, SimpleRestCreds $creds) {
  $error = '';
  $user_agent = sprintf('%s.%s %s', $this->site, $this->env, gethostname());
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
  curl_setopt($curl, CURLOPT_HEADER, 0);
  curl_setopt($curl, CURLOPT_USERPWD, $creds->name . ":" . $creds->password);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

  // If it is not a GET request, set the method here.
  if ($method != 'GET') {
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  }

  // If we are sending parameters, set the query string or POST fields here.
  $query_string = '';
  if ($method != 'GET' && !empty($parameters)) {
    $data_string = json_encode($parameters);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
      'Content-Type: application/json',
      'Content-Length: ' . strlen($data_string),
    ]);
  }
  $full_url = sprintf('%s/%s%s', $creds->url, $endpoint, $query_string);
  curl_setopt($curl, CURLOPT_URL, $full_url);
  $attempts = 0;
  $response = FALSE;
  while (!$response && ++$attempts <= $this->retryMax) {
    $response = curl_exec($curl);
    if (!$response) {
      $error = curl_error($curl);
      sleep($this->retryWait);
    }
  }
  if (!$response) {
    throw new Exception(sprintf('Error reaching url "%s" with method "%s." Returned error "%s."', $full_url, $method, $error));
  }
  $response_body = json_decode($response, TRUE);
  $response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  if (!is_array($response_body)) {
    $response_body = [];
  }
  curl_close($curl);
  return new SimpleRestResponse($endpoint, $response_code, $response_body);
}