Send a request to the server and construct an appropriate response.

@param RequestInterface $request

@return ResponseInterface
359693
public function getResponse(RequestInterface $request)
    {
        $this->write($request->getPacket());

        return $request->getResponse()->init($this->read());
    }
1145697
public function send(Request $request): Response
    {
        $this->serialInterface->send($request->encode());
        return $this->receiveResponse($request->getTimeout());
    }
693518
private function doSendRequest(RequestInterface $request)
    {
        try {
            $response = $this->getHttpClient()->sendRequest($request);
        } catch (HttpTransferException $e) {
            throw new TransferException(
                $e->getMessage(),
                $request,
                $e instanceof HttpException ? $e->getResponse() : null
            );
        }

        if ($response->getStatusCode() >= 400) {
            throw new InvalidResponseException('Bad response status code.', $request, $response);
        }

        $rawData = (string) $response->getBody();

        if (empty($rawData)) {
            return null;
        }

        $data = json_decode($rawData, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new InvalidResponseException('Could not decode the response.', $request, $response);
        }

        return $data;
    }
32417
protected function sendRequest(RequestInterface $request)
    {
        try {
            $response = $this->httpClient->send(
                $request,
                $this->getRequestOptions()
            );

            $this->multipartResources = [];

            return json_decode($response->getBody());
        } catch (RequestException $e) {
            $this->throwRequestException($e);
        }
    }
1371067
public function request(RequestInterface $request)
    {
        $uri = $request->getUri()
            ->withPath('')
            ->withQuery('')
            ->withFragment('');

        $uri = $uri . $request->getRequestTarget();

        $request = $request->withRequestTarget($uri);

        return $this->doRequest($request);
    }
1121914
protected function doRequest(RequestInterface $request)
    {
        // Create the stream context options array with the required method offset.
        $options = ['method' => $request->getMethod()];

        // Set HTTP Version
        $options['protocol_version'] = $request->getProtocolVersion();

        // If data exists let's encode it and make sure our Content-Type header is set.
        $data = (string) $request->getBody();

        if (isset($data)) {
            // If the data is a scalar value simply add it to the stream context options.
            if (is_scalar($data)) {
                $options['content'] = $data;
            } else // Otherwise we need to encode the value first.
            {
                $options['content'] = http_build_query($data);
            }

            if (!$request->getHeader('Content-Type')) {
                $request = $request->withHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
            }

            // Add the relevant headers.
            $request = $request->withHeader('Content-Length', (string) strlen($options['content']));
        }

        // Speed up stream get URL
        // @see http://stackoverflow.com/questions/3629504/php-file-get-contents-very-slow-when-using-full-url
        // @see http://stackoverflow.com/questions/13679976/how-to-speed-up-file-get-contents
        $request = $request->withHeader('Connection', 'Close');

        // Build the headers string for the request.
        if ($headers = $request->getHeaders()) {
            // Add the headers string into the stream context options array.
            $options['header'] = HeaderHelper::toHeaderLine($headers, true);
        }

        // If an explicit timeout is given user it.
        if ($this->getOption('timeout')) {
            $options['timeout'] = (int) $this->getOption('timeout');
        }

        // If an explicit user agent is given use it.
        if ($this->getOption('userAgent')) {
            $options['user_agent'] = $this->getOption('userAgent');
        }

        // Ignore HTTP errors so that we can capture them.
        $options['ignore_errors'] = 1;

        // Follow redirects.
        $options['follow_location'] = (int) $this->getOption('follow_location', 1);

        foreach ((array) $this->getOption('options') as $key => $value) {
            $options[$key] = $value;
        }

        // Create the stream context for the request.
        $context = stream_context_create(['http' => $options]);

        // Capture PHP errors
        $php_errormsg = '';
        $track_errors = ini_get('track_errors');
        ini_set('track_errors', '1');

        $connection = @fopen($request->getRequestTarget(), Stream::MODE_READ_ONLY_FROM_BEGIN, false, $context);

        if (!$connection) {
            if (!$php_errormsg) {
                // Error but nothing from php? Create our own
                $php_errormsg = sprintf('Could not connect to resource: %s', $request->getRequestTarget());
            }

            // Restore error tracking to give control to the exception handler
            ini_set('track_errors', $track_errors);

            throw new \RuntimeException($php_errormsg);
        }

        $stream = new Stream($connection);

        if ($dest = $this->getOption('target_file')) {
            $content = '';
            StreamHelper::copyTo($stream, $dest);
        } else {
            $content = $stream->getContents();
        }

        $metadata = $stream->getMetadata();

        $stream->close();

        if (isset($metadata['wrapper_data']['headers'])) {
            $headers = $metadata['wrapper_data']['headers'];
        } elseif (isset($metadata['wrapper_data'])) {
            $headers = $metadata['wrapper_data'];
        } else {
            $headers = [];
        }

        return $this->getResponse($headers, $content);
    }
373691
public function run(RequestInterface $request) : ResponseInterface
    {
        $stack = array_reverse($this->stack);
        $handler = $this->resolve($stack, 0);
        return $handler->handle($request);
    }
1208049
public function handle(RequestInterface $request): ResponseInterface
    {
        if (!array_key_exists($this->index, $this->middlewares)) {
            return $this->response;
        }

        $current = $this->index;

        ++$this->index;

        $response = $this->container->call([$this->middlewares[$current], 'process'], [$request, $this]);

        return $response;
    }
1006845
public function getParsedResponse(RequestInterface $request)
    {
        try {
            $response = $this->getResponse($request);
        } catch (BadResponseException $e) {
            $response = $e->getResponse();
        }

        $parsed = $this->parseResponse($response);

        $this->checkResponse($response, $parsed);

        return $parsed;
    }
625214
public function sendRequest(RequestInterface $request):ResponseInterface{

		// get authorization only if we request the provider API
		if(strpos((string)$request->getUri(), $this->apiURL) === 0){
			$token = $this->storage->getAccessToken($this->serviceName);

			// attempt to refresh an expired token
			if($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)){
				$token = $this->refreshAccessToken($token);
			}

			$request = $this->getRequestAuthorization($request, $token);
		}

		return $this->http->sendRequest($request);
	}
178476
public function sendRequest(RequestInterface $request)
    {
        if (false === is_resource($this->resource)) {
            $this->resource = curl_init();
        }

        $response = $this->channelBuilder
            ->channel($this->resource)
            ->options(array_replace($this->options, $this->requestOptions))
            ->request($request)
            ->response($this->responseFactory->createResponse())
            ->getChannel()
            ->send();

        $response->getBody()->rewind();
        $this->requestOptions = [];

        return $response;
    }
1282571
public function handle(RequestInterface $request)
    {
        $index = 0;

        while ($this->canSetNextCore($index)) {
            $this->middlewares[$index]->setNextCore($this->middlewares[$index + 1]);
            ++$index;
        }

        return $this->middlewares[0]->handle($request);
    }
371685
public function onRequest( RequestInterface $request ) {
        $schema = $this->getEndpoint()->getSchema();

        return $request->withHeader( 'X-Schema-Version', $schema );
    }
176759
public static function forge(RequestInterface $request)
    {
        $headerSize = $request->getInfo(CURLINFO_HEADER_SIZE);
        $response   = $request->getRawResponse();
        $content    = (strlen($response) === $headerSize) ? '' : substr($response, $headerSize);

        $rawHeaders = rtrim(substr($response, 0, $headerSize));
        $headers    = [];

        foreach (preg_split('/(\\r?\\n)/', $rawHeaders) as $header) {
            if ($header) {
                $headers[] = $header;
            } else {
                $headers = [];
            }
        }

        $headerBag = [];
        $info      = $request->getInfo();
        $status    = explode(' ', $headers[0]);
        $status    = explode('/', $status[0]);

        unset($headers[0]);

        foreach ($headers as $header) {
            $headerPair = explode(': ', $header);

            if (count($headerPair) > 1) {
                list($key, $value)     = $headerPair;
                $headerBag[trim($key)] = trim($value);
            }
        }

        $response = new static($content, $info['http_code'], $headerBag);
        $response->setProtocolVersion($status[1]);
        $response->setCharset(substr(strstr($response->headers->get('Content-Type'), '='), 1));

        return $response;
    }
1190909
public function execute(RequestInterface $request): RequestInterface
    {
        $body = $request->getBody()->getContents();

        $iv = random_bytes(self::INITIATION_VECTOR_LENGTH);
        $aesKey = random_bytes(self::AES_KEY_LENGTH);
        $encryptedAesKey = $this->publicKey->encrypt($aesKey);
        $bodyEncrypted = $this->determineBodyStringEncrypted($body, $aesKey, $iv);
        $hmac = $this->determineHmac($aesKey, $iv, $bodyEncrypted);
        $encryptedHeaders = [
            self::HEADER_CLIENT_ENCRYPTION_IV => [base64_encode($iv)],
            self::HEADER_CLIENT_ENCRYPTION_KEY => [base64_encode($encryptedAesKey)],
            self::HEADER_CLIENT_ENCRYPTION_HMAC => [base64_encode($hmac)],
        ];

        return new Request(
            $request->getMethod(),
            $request->getUri(),
            array_merge($request->getHeaders(), $encryptedHeaders),
            $bodyEncrypted
        );
    }
1294042
public function submit(RequestInterface $request)
    {
        $response = $this->browser->submit(
            $this->options['host'] . $request->getPath(),
            $request->getData(),
            $request->getMethod());

        return json_decode($response->getContent());
    }
805939
public function send(RequestInterface $request)
	{
		$request = $this->authentication($request);
		$defaultHeaders = [
			'Accept'       => $this->getContentType(),
			'Content-Type' => $this->getContentType()
		];

		foreach ($defaultHeaders as $defaultHeader => $value)
		{
			if ( ! $request->hasHeader($defaultHeader))
			{
				$request = $request->withHeader($defaultHeader, $value);
			}
		}

		$response = $this->client->sendRequest($request);
		$response = $this->transformResponseToException($request, $response);

		return $response;
	}