Skip to content

Core\Http\Response::headers

$response->headers(string $name): array<tainted string>$response->headers(
    string $name,
): array<tainted string>

Returns every line of one header of a reply, as an array of strings.

A server can send the same header on more than one line. headers() gives one string for each line, in the order the lines arrived. A header that arrived once gives an array with one string, and a missing header gives an empty array. The name does not depend on upper or lower case. Each value comes from another server, so it is tainted until your program checks it.

Use headers() to read cookies, because header() throws an error for Set-Cookie. Use it also when each line has its own meaning, for example a Link header.

Good to know: the examples use Core\Test::answerHttp to give fixed replies, so they run without a network. They read every cookie of a reply, show a missing header, and find the next page of a list in the Link headers.

Parameters

  • $namestringThe header name. Upper and lower case are treated the same, as in header.

Return value

Returns array<tainted string>. One tainted entry per header line, in the order they arrived. The result is an empty array if the reply has no such header. A header sent once gives an array with one entry.

Examples

Reading every cookie

<?nvs
// Shows how to read a header that a server sends on more than one line, here
// the cookies of a reply.
// `Core\Test::answerHttp` gives a fixed reply with two `Set-Cookie` lines, so the
// example runs without a network.
array<string> $cookies = ['session=8f2c; HttpOnly', 'theme=dark; Expires=Wed, 21 Oct 2026 07:28:00 GMT'];
Core\Test::answerHttp('https://shop.example.com/login', 200, { headers: ['Set-Cookie' => $cookies] });
Core\Http\Response $response = Core\Http\Client::get('https://shop.example.com/login');
// `headers()` returns one string for each line, in the order they arrived.
// This prints the two cookies on two lines.
foreach ($response->headers('set-cookie') as tainted string $cookie) {
echo $cookie, "\n";
}
Output
session=8f2c; HttpOnly
theme=dark; Expires=Wed, 21 Oct 2026 07:28:00 GMT

A header that is missing

<?nvs
// Shows what `headers()` returns for a header that the reply has once, and for
// a header that the reply does not have.
Core\Test::answerHttp('https://api.example.com/status', 200, { headers: ['Vary' => 'Accept'] });
Core\Http\Response $response = Core\Http\Client::get('https://api.example.com/status');
// A header that arrived once gives an array with one string. This prints 1.
echo Core\Arr::count($response->headers('Vary')), "\n";
// A missing header gives an empty array, not `null`. This prints 0.
echo Core\Arr::count($response->headers('Warning')), "\n";
Output
1
0

Finding the next page

<?nvs
// Shows a program that reads a long list from an API one page at a time. Each
// reply has `Link` headers, and the one marked `rel="next"` gives the next page.
class Pages
{
// Returns the address of the next page, or `null` on the last page.
public static function next(Core\Http\Response $response): ?tainted string
{
foreach ($response->headers('Link') as tainted string $link) {
// A link is written as `<address>; rel="next"`. The address is the
// text after `<` and before `>`.
if (Core\Str::endsWith($link, '; rel="next"')) {
tainted string $start = Core\Str::before($link, '>') ?? '';
return Core\Str::after($start, '<');
}
}
return null;
}
}
// Two fixed replies, so the example runs without a network. The first page has
// a link to the second page, and the second page is the last one.
array<string> $links = [
'<https://api.example.com/orders?page=1>; rel="first"',
'<https://api.example.com/orders?page=2>; rel="next"',
];
Core\Test::answerHttp('https://api.example.com/orders?page=1', 200, { headers: ['Link' => $links] });
Core\Test::answerHttp('https://api.example.com/orders?page=2', 200, {});
// This prints the address of page 2, then `last page`.
Core\Http\Response $first = Core\Http\Client::get('https://api.example.com/orders?page=1');
echo Pages::next($first) ?? 'last page', "\n";
Core\Http\Response $second = Core\Http\Client::get('https://api.example.com/orders?page=2');
echo Pages::next($second) ?? 'last page', "\n";
Output
https://api.example.com/orders?page=2
last page