chobie\Jira\Api\Client\CurlClient::sendRequest PHP Method

sendRequest() public method

Sends request to the API server.
public sendRequest ( string $method, string $url, array | string $data = [], string $endpoint, chobie\Jira\Api\Authentication\AuthenticationInterface $credential, boolean $is_file = false, boolean $debug = false ) : array | string
$method string Request method.
$url string URL.
$data array | string Request data.
$endpoint string Endpoint.
$credential chobie\Jira\Api\Authentication\AuthenticationInterface Credential.
$is_file boolean This is a file upload request.
$debug boolean Debug this request.
return array | string
    public function sendRequest($method, $url, $data = array(), $endpoint, AuthenticationInterface $credential, $is_file = false, $debug = false)
    {
        if (!$credential instanceof Basic && !$credential instanceof Anonymous) {
            throw new \InvalidArgumentException(sprintf('CurlClient does not support %s authentication.', get_class($credential)));
        }
        $curl = curl_init();
        if ($method == 'GET') {
            if (!is_array($data)) {
                throw new \InvalidArgumentException('Data must be an array.');
            }
            $url .= '?' . http_build_query($data);
        }
        curl_setopt($curl, CURLOPT_URL, $endpoint . $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if (!$credential instanceof Anonymous) {
            curl_setopt($curl, CURLOPT_USERPWD, sprintf('%s:%s', $credential->getId(), $credential->getPassword()));
        }
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_VERBOSE, $debug);
        if ($is_file) {
            if (defined('CURLOPT_SAFE_UPLOAD') && PHP_VERSION_ID < 70000) {
                curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
            }
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Atlassian-Token: nocheck'));
        } else {
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8'));
        }
        if ($method == 'POST') {
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($is_file) {
                $data['file'] = $this->getCurlValue($data['file'], $data['name']);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            } else {
                curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
            }
        } elseif ($method == 'PUT' || $method == 'DELETE') {
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        }
        $response = curl_exec($curl);
        $error_number = curl_errno($curl);
        if ($error_number > 0) {
            throw new Exception(sprintf('Jira request failed: code = %s, "%s"', $error_number, curl_error($curl)));
        }
        $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        // If empty result and status != "204 No Content".
        if ($http_code == 401) {
            throw new UnauthorizedException('Unauthorized');
        }
        if ($response === '' && !in_array($http_code, array(201, 204))) {
            throw new Exception('JIRA Rest server returns unexpected result.');
        }
        // @codeCoverageIgnoreStart
        if (is_null($response)) {
            throw new Exception('JIRA Rest server returns unexpected result.');
        }
        // @codeCoverageIgnoreEnd
        return $response;
    }