protected function AcsfMessageRest::sendMessage in Acquia Cloud Site Factory Connector 8.2
Same name and namespace in other branches
- 8 src/AcsfMessageRest.php \Drupal\acsf\AcsfMessageRest::sendMessage()
Implements AcsfMessage::sendMessage().
Overrides AcsfMessage::sendMessage
File
- src/
AcsfMessageRest.php, line 39
Class
- AcsfMessageRest
- This class is an implementation of our XML-RPC service.
Namespace
Drupal\acsfCode
protected function sendMessage($url, $method, $endpoint, array $parameters, $username, $password) {
$useragent = sprintf('%s.%s %s', $this->ahSite, $this->ahEnv, gethostname());
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
// @todo can we remove this in prod?
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)) {
$query_string = '?' . UrlHelper::buildQuery($parameters);
}
elseif (!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', $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);
\Drupal::logger('AcsfMessageRest')
->error($error);
sleep($this->retryWait);
}
}
if (!$response) {
throw new AcsfMessageFailureException(sprintf('Error reaching url "%s" with method "%s." Returned error "%s."', $full_url, $method, $error));
}
$response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response_body = json_decode($response, TRUE);
return new AcsfMessageResponseRest($endpoint, $response_code, $response_body);
}