public function exec($context, $post_data = null, $custom_request = null)
{
$url = $this->createUrlByContext($context);
$this->log->addDebug("Curl {$url} JsonData=" . $post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
// post_data
if (!is_null($post_data)) {
// PUT REQUEST
if (!is_null($custom_request) && $custom_request == 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
if (!is_null($custom_request) && $custom_request == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
} else {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
}
$this->authorization($ch);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost());
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: */*', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_VERBOSE, $this->getConfiguration()->isCurlOptVerbose());
$this->log->addDebug('Curl exec=' . $url);
$response = curl_exec($ch);
// if request failed.
if (!$response) {
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$body = curl_error($ch);
curl_close($ch);
//The server successfully processed the request, but is not returning any content.
if ($this->http_response == 204) {
return '';
}
// HostNotFound, No route to Host, etc Network error
$this->log->addError('CURL Error: = ' . $body);
throw new JiraException('CURL Error: = ' . $body);
} else {
// if request was ok, parsing http response code.
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
if ($this->http_response != 200 && $this->http_response != 201) {
throw new JiraException('CURL HTTP Request Failed: Status Code : ' . $this->http_response . ', URL:' . $url . "\nError Message : " . $response, $this->http_response);
}
}
return $response;
}