PHP Luminova: HTTP Response Message Stream
Provides a stream interface for handling HTTP response data, supporting reading, writing, seeking, and metadata management.
The Luminova Message Stream class is a File Stream wrapper designed for working with HTTP response stream resources.
It fully complies with PSR-7 Psr\Http\Message\StreamInterface standards, ensuring seamless compatibility with Luminova’s HTTP Novio Client. Since it extends all features of File Stream, you can use methods like rewind(), seek(), tell(), and more.
NoteThe HTTP response stream is read-only by default.To write data into the stream, call
setReadOnly(false)before performing any write operations.
For a complete list of available methods, see the File Stream documentation.
Examples
Using streams with Luminova Novio Client
use Luminova\HTTP\Client\Novio;
$client = new Novio();
$response = $client->request('GET', 'https://example.com/api/data');
// Directly work with the stream
$body = $response->getBody();
$data = (string) $body;
echo $data;Reading the full HTTP response body
// Assume $response is a Luminova HTTP Response object
// Returns: Luminova\Http\Message\Response
$stream = $response->getBody();
// Convert the stream to a string
echo (string) $stream;
// All content
echo $stream->buffer();
// Transform to array
print_r($stream->toArray());
// Transform to object
print_r($stream->toObject());
// Json if response is json string
$stream->isJson()Reading the stream in chunks
$stream = $response->getBody();
while (!$stream->eof()) {
$chunk = $stream->read(1024); // read 1024 bytes at a time
echo $chunk;
}Writing to a stream
use Luminova\Http\Message\Stream;
$stream->setReadOnly(false);
$stream->write("Hello world!\n");
$stream->rewind(); // reset pointer to the beginning
echo (string) $stream; // outputs: Hello world!Creating a new stream
use Luminova\Http\Message\Stream;
$stream = new Stream(fopen('php://temp', 'r+'));
$stream->write("Hello world!\n");
$stream->rewind(); // reset pointer to the beginning
echo (string) $stream; // outputs: Hello world!Seeking and getting metadata
$stream = $response->getBody();
// Move to the start of the stream
$stream->seek(0);
// Get the stream metadata
$meta = $stream->getMetadata();
print_r($meta);Class Definition
- Class namespace:
Luminova\Http\Message\Stream - Parent namespace: Luminova\Storage\Stream
- This class implements: Psr\Http\Message\StreamInterface, Stringable
Methods
constructor
Create a new HTTP response message stream from PHP resource.
public __construct(resource $resource): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$resource | resource | A valid PHP stream resource (e.g., from fopen('php://temp')). |
Throws:
- \Luminova\Exceptions\InvalidArgumentException - If the provided value is not a valid stream resource.
buffer
Read the entire stream content, always starting from the beginning.
If the stream is seekable the pointer is rewound to position 0 before reading, so previously consumed content is included in the result.
For non-seekable streams only the remaining (unread) bytes are returned.
public buffer(): stringReturn Value:
string - Returns the complete stream content (or remaining content for non-seekable streams).
Throws:
- \Luminova\Exceptions\RuntimeException - If the stream cannot be read.
Useful when you want the whole response body regardless of whether it has been partially read earlier.
toString
Reads the entire stream and returns it contents as string.
public toString(): stringReturn Value:
string - Return the contents of the stream.
isSeekable
Checks if the stream is seekable.
public isSeekable(): boolReturn Value:
bool - Return true if the stream is seekable, false otherwise.
isWritable
Checks if the stream is writable.
public isWritable(): boolReturn Value:
bool - Return true if the stream is writable, false otherwise.
isReadable
Checks if the stream is readable.
public isReadable(): boolReturn Value:
bool - Return true if the stream is readable, false otherwise.
getSize
Return the total size of the stream content in bytes.
The result is cached after the first successful call and invalidated whenever write() modifies the stream.
public getSize(): ?intReturn Value:
int|null - Return the stream size in bytes, or null if unknown.
getUri
Return the URI or filename associated with the stream, if available.
For php://temp and file-based streams this is the underlying path or wrapper URI. For php://memory and data:// streams it is the scheme URI.
Returns null when the stream has been detached or no URI exists.
public getUri(): ?stringReturn Value:
int|null - Return the stream URI, or null if unavailable.
getMetadata
Retrieve stream metadata or a single metadata value by key.
If stream has been detached, returns null, for a keyed look-up or an empty array for a full look-up.
public getMetadata(?string $key = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string|null | A specific metadata key (e.g. mode, seekable, uri), or nullto retrieve the full associative array. |
Return Value:
mixed - Return the value for the requested key, the full metadata array when $key is null, or null / [] if the stream is detached.
getContents
Return all bytes from the current stream position to EOF.
Unlike buffer(), this method does not rewind first; it reads only the bytes that remain after the current pointer position.
public getContents(): stringReturn Value:
mixed - Return the remaining stream content.
Throws:
- \Luminova\Exceptions\RuntimeException - If the stream has been detached, is not readable, or fails.