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 function __construct(mixed $resource = null)Parameters:
| 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 function buffer(): stringReturn:
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 function toString(): stringReturn:
string - Return the contents of the stream.
isSeekable
Checks if the stream is seekable.
public function isSeekable(): boolReturn:
bool - Return true if the stream is seekable, false otherwise.
isWritable
Checks if the stream is writable.
public function isWritable(): boolReturn:
bool - Return true if the stream is writable, false otherwise.
isReadable
Checks if the stream is readable.
public function isReadable(): boolReturn:
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 function getSize(): ?intReturn:
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 function getUri(): ?stringReturn:
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 function 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:
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 function getContents(): stringReturn:
mixed - Return the remaining stream content.
Throws:
- Luminova\Exceptions\RuntimeException - If the stream has been detached, is not readable, or fails.
write
Write data at the current stream position and invalidate the cached size.
public function write(string $string): intParameters:
| Parameter | Type | Description |
|---|---|---|
$string | string | Data to write. |
Return:
int - Returns the number of bytes written.
close
Close the underlying stream and invalidate the cached size.
public function close(): voiddetach
Detach the underlying stream without closing it. The caller owns the returned resource.
public function detach(): mixedReturn:
resource|null - Returns the detached resource, or null if no resource is attached.