Core\Http\Stream::headers
$stream->headers(string $name): array<tainted string>$stream->headers( string $name, ): array<tainted string>
Returns every line of one header of a streamed reply, as a list.
A server can send the same header on more than one line. headers() returns one tainted string for each line, in the order they arrived. A header that was sent once gives a list with one entry. A header that was not sent gives an empty list. The name can be written in any case.
header() joins the lines into one text. Use headers() when the lines must stay apart. This is always the case for set-cookie, because one cookie can contain a comma. For this reason header("set-cookie") throws a LogicError, and headers("set-cookie") works.
The examples below read every cookie of a reply, count the lines of three headers, and build the cookie header for the next request.
Parameters
$namestringThe header name. Upper and lower case do not matter.
Return value
Returns array<tainted string>. The values in the order they arrived, each tainted. A header that arrived once gives a list of one value. A header that never arrived gives an empty list.
Examples
Reading every cookie
<?nvs// Shows how to read every cookie that a streamed reply sets.
// The server sends two cookies. Each one is on its own `set-cookie` line.array<string> $cookies = ['session=abc123; Path=/', 'theme=dark; Max-Age=3600'];Core\Test::answerHttp('https://shop.example.com/login', 200, { body: "welcome\n", headers: ['set-cookie' => $cookies],});
Core\Http\Stream $login = Core\Http\Client::openStream(Core\Http\Method::Post, 'https://shop.example.com/login');// `headers()` returns one entry for each line, in the order they arrived.// This prints the two cookies on two lines.foreach ($login->headers('set-cookie') as tainted string $cookie) { echo $cookie, "\n";}session=abc123; Path=/theme=dark; Max-Age=3600One line two lines and none
<?nvs// Shows what `headers()` returns for a header sent once, sent twice, or not sent at all.
array<string> $vary = ['Accept', 'Accept-Encoding'];Core\Test::answerHttp('https://api.example.com/export', 200, { body: "id,name\n", headers: ['content-type' => 'text/csv', 'vary' => $vary],});
Core\Http\Stream $export = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://api.example.com/export');// The name can be written in any case. This prints 1, 2 and 0.echo Core\Arr::count($export->headers('Content-Type')), "\n";echo Core\Arr::count($export->headers('vary')), "\n";// The server did not send `etag`, so the result is an empty list.echo Core\Arr::count($export->headers('etag')), "\n";120Sending the cookies back
<?nvs// Shows how to build the `cookie` header for the next request from the cookies a server set.
array<string> $cookies = ['session=abc123; Path=/; HttpOnly', 'cart=7; Max-Age=600'];Core\Test::answerHttp('https://shop.example.com/login', 200, { body: '', headers: ['set-cookie' => $cookies],});
Core\Http\Stream $login = Core\Http\Client::openStream(Core\Http\Method::Post, 'https://shop.example.com/login');tainted string $header = '';foreach ($login->headers('set-cookie') as tainted string $line) { // The part before the first ";" is the name and the value. The rest are settings. array<tainted string> $parts = Core\Str::split($line, ';'); if ($header != '') { $header = $header . '; '; } $header = $header . $parts[0];}
// This prints "session=abc123; cart=7".echo $header, "\n";session=abc123; cart=7