src/ChrisKonnertz/DeepLy/HttpClient/CurlHttpClient.php
public function callApi($url, array $payload, $method)
    {
        if (! is_string($url)) {
            throw new \InvalidArgumentException('$url has to be a string');
        }
        if (! is_string($method)) {
            throw new \InvalidArgumentException('$method has to be a string');
        }

        $jsonData = $this->protocol->createRequestData($payload, $method);

        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->sslVerifyPeer);

        // Note: We assume that we always will use JSON to encode data
        // so this is independent from the protocol that we actually use
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($jsonData)) // Note: We do not need mb_strlen here since JSON encodes Unicode
        );

        $rawResponseData = curl_exec($curl);

        if ($rawResponseData === false) {
            $exception = new CallException('cURL error during DeepLy API call: '.curl_error($curl));
            throw $exception;
        }

        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($code !== 200) {
            // Note that the response probably will contain an error description wrapped in a HTML page.
            // We extract the text and display add it tot he exception message.
            $text = $this->getTextFromHtml($rawResponseData);
            if ($text !== null) {
                $text = ', message: "'.$text.'"';
            }

            throw new CallException('Server side error during DeepLy API call: HTTP code '.$code.$text);
        }
        
        curl_close($curl);

        return $rawResponseData;
    }
public function callApi($url, array $payload, $method)
    {
        if (! is_string($url)) {
            throw new \InvalidArgumentException('$url has to be a string');
        }
        if (! is_string($method)) {
            throw new \InvalidArgumentException('$method has to be a string');
        }

        $jsonData = $this->protocol->createRequestData($payload, $method);

        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->sslVerifyPeer);

        // Note: We assume that we always will use JSON to encode data
        // so this is independent from the protocol that we actually use
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($jsonData)) // Note: We do not need mb_strlen here since JSON encodes Unicode
        );

        $rawResponseData = curl_exec($curl);

        if ($rawResponseData === false) {
            $exception = new CallException('cURL error during DeepLy API call: '.curl_error($curl));
            throw $exception;
        }

        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($code !== 200) {
            // Note that the response probably will contain an error description wrapped in a HTML page.
            // We extract the text and display add it tot he exception message.
            $text = $this->getTextFromHtml($rawResponseData);
            if ($text !== null) {
                $text = ', message: "'.$text.'"';
            }

            throw new CallException('Server side error during DeepLy API call: HTTP code '.$code.$text);
        }
        
        curl_close($curl);

        return $rawResponseData;
    }
Executes an API call (a request) and returns the raw response data

@param  string $url     The URL of the API endpoint
@param  array  $payload The payload of the request. Will be encoded as JSON
@param  string $method  The name of the method of the API call
@return string          The raw response data as string (usually contains stringified JSON)
@throws CallException   Throws a call exception if the call could not be executed