Core\Http\Stream::header
Returns one header of a streamed reply, or null when the server did not send it.
The name is matched without regard to case, so Content-Type and content-type find the same header. When the server sent the same header on several lines, header() joins the values with a comma and a space. The value came from another server, so it is tainted.
The headers arrive before the body. So you can read them before you decide how to read the body, or whether to read it at all. Reading a header does not read the body, and you can still read headers after the body is read.
Good to know: Set-Cookie cannot be joined safely, so header("set-cookie") throws a LogicError. Use headers() for it, which returns one value for each line.
The examples below read one header, show a header sent twice and one that is missing, and check the size of a download before it starts.
Parameters
$namestringThe header name. Upper and lower case do not matter.
Return value
Returns ?tainted string. The header value, or null when the reply has no such header. The value is tainted, which means it came from outside your program, like every byte of a reply.
Errors
LogicErrorThe name isSet-Cookie. Its values cannot be joined, so useheadersto read them.
Examples
Reading the content type
<?nvs// Shows how to read one header of a streamed reply.
// The reply comes from this table, so nothing is sent over a network.Core\Test::answerHttp('https://feed.example.com/news', 200, { body: "data: hello\n\n", headers: ['content-type' => 'text/event-stream'],});
Core\Http\Stream $news = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://feed.example.com/news');// The name can be written in any case. This prints "text/event-stream".echo $news->header('Content-Type') ?? 'no content type', "\n";text/event-streamA header sent twice and a missing one
<?nvs// Shows what `header()` returns for a header sent on two lines, and for a missing one.
// The server sends the `cache-control` header on two lines.array<string> $cache = ['no-cache', 'no-store'];Core\Test::answerHttp('https://api.example.com/export', 200, { body: "id,name\n", headers: ['cache-control' => $cache],});
Core\Http\Stream $export = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://api.example.com/export');// The two values are joined. This prints "no-cache, no-store".echo $export->header('cache-control') ?? 'none', "\n";// The server did not send `etag`, so `header()` returns `null`. This prints "none".echo $export->header('etag') ?? 'none', "\n";no-cache, no-storenoneChecking the size before a download
<?nvs// Shows how to check the size of a download before any of its body is read.
// The server says the file has 5000000 bytes. The program allows at most 1000000.Core\Test::answerHttp('https://files.example.com/backup.zip', 200, { body: 'PK', headers: ['content-length' => '5000000'],});
uint $limit = 1000000;Core\Http\Stream $download = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://files.example.com/backup.zip');// The header is `tainted`. `as ?int` converts it to a whole number.// If that is not possible, the result is `null`.?int $size = ($download->header('content-length') ?? '') as ?int;
// This prints "too large: 5000000 bytes". The body is never read.if ($size == null) { echo "the server did not say the size\n";} elseif ($size > $limit) { echo 'too large: ', $size, " bytes\n";} else { $download->saveTo('backup.zip', $limit); echo "saved\n";}too large: 5000000 bytes