protected function readResponse(RequestInterface $request, $socket): ResponseInterface
    {
        $headers = [];
        $reason = null;

        while (false !== ($line = fgets($socket))) {
            if ('' === rtrim($line)) {
                break;
            }
            $headers[] = trim($line);
        }

        $metadatas = stream_get_meta_data($socket);

        if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) {
            throw new TimeoutException('Error while reading response, stream timed out', $request, null);
        }

        $parts = explode(' ', array_shift($headers), 3);

        if (count($parts) <= 1) {
            throw new BrokenPipeException('Cannot read the response', $request);
        }

        $protocol = substr($parts[0], -3);
        $status = $parts[1];

        if (isset($parts[2])) {
            $reason = $parts[2];
        }

        // Set the size on the stream if it was returned in the response
        $responseHeaders = [];

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

            if (!array_key_exists(trim($headerParts[0]), $responseHeaders)) {
                $responseHeaders[trim($headerParts[0])] = [];
            }

            $responseHeaders[trim($headerParts[0])][] = isset($headerParts[1])
                ? trim($headerParts[1])
                : '';
        }

        $response = new Response($status, $responseHeaders, null, $protocol, $reason);
        $stream = $this->createStream($socket, $request, $response);

        return $response->withBody($stream);
    }
protected function readResponse(RequestInterface $request, $socket): ResponseInterface
    {
        $headers = [];
        $reason = null;

        while (false !== ($line = fgets($socket))) {
            if ('' === rtrim($line)) {
                break;
            }
            $headers[] = trim($line);
        }

        $metadatas = stream_get_meta_data($socket);

        if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) {
            throw new TimeoutException('Error while reading response, stream timed out', $request, null);
        }

        $parts = explode(' ', array_shift($headers), 3);

        if (count($parts) <= 1) {
            throw new BrokenPipeException('Cannot read the response', $request);
        }

        $protocol = substr($parts[0], -3);
        $status = $parts[1];

        if (isset($parts[2])) {
            $reason = $parts[2];
        }

        // Set the size on the stream if it was returned in the response
        $responseHeaders = [];

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

            if (!array_key_exists(trim($headerParts[0]), $responseHeaders)) {
                $responseHeaders[trim($headerParts[0])] = [];
            }

            $responseHeaders[trim($headerParts[0])][] = isset($headerParts[1])
                ? trim($headerParts[1])
                : '';
        }

        $response = new Response($status, $responseHeaders, null, $protocol, $reason);
        $stream = $this->createStream($socket, $request, $response);

        return $response->withBody($stream);
    }
protected function readResponse ( RequestInterface $ request , $ socket ) : ResponseInterface { $ headers = [ ] ; $ reason = null ; while ( false !== ( $ line = fgets ( $ socket ) ) ) { if ( '' === rtrim ( $ line ) ) { break ; } $ headers [ ] = trim ( $ line ) ; } $ metadatas = stream_get_meta_data ( $ socket ) ; if ( array_key_exists ( 'timed_out' , $ metadatas ) && true === $ metadatas [ 'timed_out' ] ) { throw new TimeoutException ( 'Error while reading response, stream timed out' , $ request , null ) ; } $ parts = explode ( ' ' , array_shift ( $ headers ) , 3 ) ; if ( count ( $ parts ) <= 1 ) { throw new BrokenPipeException ( 'Cannot read the response' , $ request ) ; } $ protocol = substr ( $ parts [ 0 ] , - 3 ) ; $ status = $ parts [ 1 ] ; if ( isset ( $ parts [ 2 ] ) ) { $ reason = $ parts [ 2 ] ; } // Set the size on the stream if it was returned in the response $ responseHeaders = [ ] ; foreach ( $ headers as $ header ) { $ headerParts = explode ( ':' , $ header , 2 ) ; if ( ! array_key_exists ( trim ( $ headerParts [ 0 ] ) , $ responseHeaders ) ) { $ responseHeaders [ trim ( $ headerParts [ 0 ] ) ] = [ ] ; } $ responseHeaders [ trim ( $ headerParts [ 0 ] ) ] [ ] = isset ( $ headerParts [ 1 ] ) ? trim ( $ headerParts [ 1 ] ) : '' ; } $ response = new Response ( $ status , $ responseHeaders , null , $ protocol , $ reason ) ; $ stream = $ this -> createStream ( $ socket , $ request , $ response ) ; return $ response -> withBody ( $ stream ) ; }
Read a response from a socket.

@param resource $socket

@throws TimeoutException    When the socket timed out
@throws BrokenPipeException When the response cannot be read
Read a response from a socket .