Luminova Framework

PHP Luminova: HTTP Response Object

Last updated: 2026-11-28 20:57:12

Handles HTTP responses from network requests via HTTP clients like, Novio, or Guzzle, providing easy access to status codes, headers, and body content.

The HTTP Response object in Luminova encapsulates the result of a network request made through Novio, Guzzle-Helper. It provides convenient methods to access the response's status code, headers, body, and other details, allowing you to easily inspect, debug, or process server responses in your application.


Class Definition


Methods

constructor

Creates a new HTTP response object.

public function __construct(
    StreamInterface|string $body = '',
    int $statusCode = 200,
    array $headers = [],
    array $info = [],
    string $reasonPhrase = 'OK',
    string $protocolVersion = '1.1',
    ?CookieJarInterface $cookie = null
)

Parameters:

ParameterTypeDescription
$streamStreamInterface|stringOptional stream object as response.
$statusCodeintThe HTTP status code of the response (default: 200).
$headersarray<string,mixed>An array of response headers.
$infoarray<string,mixed>Additional response information from cURL (optional).
$reasonPhrasestringReason phrase associated with the status code (default: OK).
$protocolVersionstringThe HTTP protocol version (default: '1.1').
$cookieCookieJarInterface|nullOptionally HTTP cookie jar object.

toString

Convert the HTTP response to a formatted string.

This method generates the complete HTTP response string, including the status line, headers, and body content. It checks for the 'Content-Length' header and adds it if not present.

public function toString(): string

Return:

string - Return the complete HTTP response as a string.


getProtocolVersion

Retrieves the HTTP protocol version (e.g., 1.0, 1.1). Returns an empty string if not set.

public function getProtocolVersion(): string

Return:

string - The HTTP protocol version.


getReasonPhrase

Retrieves the reason phrase associated with the HTTP status code (e.g., OK, Internal Server Error).

public function getReasonPhrase(): string

Return:

string - Returns the HTTP message phrase.


getStatusCode

Retrieves the HTTP status code of the response.

public function getStatusCode(): int

Return:

int - Returns the HTTP status code.


getLength

Retrieves the request response contents length.

public function getLength(?string $encoding = null): int

Parameters:

ParameterTypeDescription
$encodingstring\|nullOptional encoding argument. The response implementation counts bytes.

Return:

int - Returns the content length.


getFileTime

Retrieves the file modified time for the request.

public function getFileTime(): int

Return:

int - Returns the file modified time if available; otherwise, returns -1.


getHeaders

Retrieves all response headers.

public function getHeaders(): array

Return:

array<string, array<array-key, mixed>> - Returns an associative array of response headers.


getHeader

Retrieves a specific header value by its key.

public function getHeader(string $name): array

Parameters:

ParameterTypeDescription
$namestringThe header key to retrieve.

Return:

array<int, mixed> - Returns an array of header values.


getHeadersString

Convert an associative array of headers into a formatted string.This method converts the response headers to a string representation suitable for HTTP responses, where each header is formatted as key: value\r\n and separated by CRLF Carriage Return (ASCII 13, \r ) Line Feed (ASCII 10, \n).

public function getHeadersString(): string

Return:

string - Return a formatted string containing all headers, followed by an additional CRLF to signal the end of the headers section.


getHeaderLine

Retrieves a comma-separated string of values for a specific header.

public function getHeaderLine(string $header): string

Parameters:

ParameterTypeDescription
$headerstringThe header key name to retrieve.

Return:

string - Returns a string of concatenated values for the given header.


hasHeader

Checks if a specific header exists in the response.

public function hasHeader(string $name): bool

Parameters:

ParameterTypeDescription
$namestringThe header name to check.

Return:

bool - Returns true if the header exists, false otherwise.


getBody

Retrieves the response body as a stream. This method opens a temporary stream, writes the response contents if available (and if not already a stream), and then rewinds the stream before returning it.

public function getBody(): StreamInterface

Return:

Luminova\Http\Message\Stream<StreamInterface> - The response body as a stream object.

Throws:

See Also:

For more information on stream management, refer to:


getContents

Retrieves the extracted response contents as a string. Use this only for non-stream request responses.

public function getContents(): string

Return:

string - Returns the processed response contents.


getInfo

Retrieves additional information about the response.

public function getInfo(): array

Return:

array<string, mixed> - Returns an associative array of response metadata.


withHeader

Returns a new instance with the specified header value replacing the existing one.

/**
 * @param string|string[] $value
 */
public function withHeader(string $name, $value): static

Parameters:

ParameterTypeDescription
$namestringCase-insensitive header field name.
$valuestring|string[]Header value(s).

Return:

ResponseInterface - A new response object with the updated header.

Throws:


withoutHeader

Returns a new instance without the specified header.

public function withoutHeader(string $name): static

Parameters:

ParameterTypeDescription
$namestringCase-insensitive header field name to remove.

Return:

ResponseInterface - Returns a new response object without the specified header.


withStatus

Returns a new instance with the specified status code and optional reason phrase.

public function withStatus(int $code, string $reasonPhrase = ''): static

Parameters:

ParameterTypeDescription
$codeintThe 3-digit HTTP status code (e.g., 1xx to 5xx).
$reasonPhrasestringOptional reason phrase.

Return:

ResponseInterface - A new response object with the specified status.

Throws:


withProtocolVersion

Returns a new instance with the specified HTTP protocol version.

public function withProtocolVersion(string $version): static

Parameters:

ParameterTypeDescription
$versionstringHTTP protocol version (e.g., 1.1, 1.0).

Return:

ResponseInterface - A new response object with the specified protocol version.


withAddedHeader

Append values to a response header on a clone. Header names are case-insensitive.

public function withAddedHeader(string $name, $value): static

Parameters:

ParameterTypeDescription
$namestringHeader field name.
$valuestring\|string[]One value or a non-empty list of values to append.

Return:

static - Returns a copy with the added values.

Throws:

  • Luminova\Exceptions\InvalidArgumentException - If a header name or value is invalid.

withBody

Replace the response body on a clone.

public function withBody(StreamInterface $body): static

Parameters:

ParameterTypeDescription
$bodyPsr\Http\Message\StreamInterfaceReplacement body stream.

Return:

static - Returns a copy with the supplied stream.