DrewM\MailChimp\MailChimp::makeRequest PHP Method

makeRequest() private method

Performs the underlying HTTP request. Not very exciting.
private makeRequest ( string $http_verb, string $method, array $args = [], integer $timeout = 10 ) : array | false
$http_verb string The HTTP verb to use: get, post, put, patch, delete
$method string The API method to be called
$args array Assoc array of parameters to be passed
$timeout integer
return array | false Assoc array of decoded result
    private function makeRequest($http_verb, $method, $args = array(), $timeout = 10)
    {
        if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
            throw new \Exception("cURL support is required, but can't be found.");
        }
        $url = $this->api_endpoint . '/' . $method;
        $this->last_error = '';
        $this->request_successful = false;
        $response = array('headers' => null, 'body' => null);
        $this->last_response = $response;
        $this->last_request = array('method' => $http_verb, 'path' => $method, 'url' => $url, 'body' => '', 'timeout' => $timeout);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.api+json', 'Content-Type: application/vnd.api+json', 'Authorization: apikey ' . $this->api_key));
        curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        switch ($http_verb) {
            case 'post':
                curl_setopt($ch, CURLOPT_POST, true);
                $this->attachRequestPayload($ch, $args);
                break;
            case 'get':
                $query = http_build_query($args, '', '&');
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $query);
                break;
            case 'delete':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
            case 'patch':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
                $this->attachRequestPayload($ch, $args);
                break;
            case 'put':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
                $this->attachRequestPayload($ch, $args);
                break;
        }
        $response['body'] = curl_exec($ch);
        $response['headers'] = curl_getinfo($ch);
        if (isset($response['headers']['request_header'])) {
            $this->last_request['headers'] = $response['headers']['request_header'];
        }
        if ($response['body'] === false) {
            $this->last_error = curl_error($ch);
        }
        curl_close($ch);
        $formattedResponse = $this->formatResponse($response);
        $this->determineSuccess($response, $formattedResponse);
        return $formattedResponse;
    }