Skip to content

Core\Http\Response::header

$response->header(string $name): ?tainted string$response->header(
    string $name,
): ?tainted string

Returns one header of a reply as a string, or null when the reply does not have that header.

The name does not depend on upper or lower case, so content-type and Content-Type give the same result. When a server sends the same header on more than one line, header() joins the lines with , in the order they arrived. The value comes from another server, so it is tainted until your program checks it.

Set-Cookie is the one header that header() does not read. Two cookies joined with a comma cannot be read correctly, so the call throws a LogicError. Use headers() for cookies.

Good to know: the examples use Core\Test::answerHttp to give fixed replies, so they run without a network. They read a content type, show a header sent twice, and read Retry-After from a service that is busy.

Parameters

  • $namestringThe header name. Upper and lower case are treated the same, as RFC 9110 § 5.1 defines.

Return value

Returns ?tainted string. The header's value, as a tainted string. If the server sent the header more than once, the values are joined with , in the order they arrived. The result is null if the reply has no such header.

Errors

  • LogicErrorThe name is Set-Cookie. Two cookies joined into one string are not valid cookies, so this method does not join them. Use headers to read Set-Cookie.

Examples

Reading the content type

<?nvs
// Shows how to read one header of a reply, and what you get for a header the
// reply does not have.
// `Core\Test::answerHttp` gives a fixed reply to this address, so the example runs
// without a network.
Core\Test::answerHttp('https://api.example.com/report', 200, {
body: 'id,total',
headers: ['Content-Type' => 'text/csv'],
});
Core\Http\Response $response = Core\Http\Client::get('https://api.example.com/report');
// The name does not depend on upper or lower case. This prints `text/csv`.
echo $response->header('content-type'), "\n";
// The reply has no `ETag` header, so `header()` returns `null`. This prints `none`.
echo $response->header('ETag') ?? 'none', "\n";
Output
text/csv
none

A header sent twice

<?nvs
// Shows what `header()` returns when a server sends the same header on two lines.
// This fixed reply has two `Cache-Control` lines and two `Set-Cookie` lines.
array<string> $caching = ['no-store', 'private'];
array<string> $cookies = ['cart=42', 'theme=dark'];
Core\Test::answerHttp('https://shop.example.com/cart', 200, {
headers: ['Cache-Control' => $caching, 'Set-Cookie' => $cookies],
});
Core\Http\Response $response = Core\Http\Client::get('https://shop.example.com/cart');
// `header()` joins the two lines with a comma. This prints `no-store, private`.
echo $response->header('Cache-Control'), "\n";
// Two cookies joined with a comma cannot be read correctly. So `header()` throws
// a `LogicError` for `Set-Cookie`. Use `headers()` to read cookies.
try {
echo $response->header('Set-Cookie'), "\n";
} catch (LogicError $error) {
echo 'header() failed: ', $error->message, "\n";
}
Output
no-store, private
header() failed: Core\Http\Response::header(): a `Set-Cookie` line is not part of RFC 9110 § 5.3's comma equivalence and two of them joined parse as neither cookie — `headers("set-cookie")` answers them one line each

Waiting when a service is busy

<?nvs
// Shows a program that calls a service which limits how often it may be called.
// When the service is busy, its reply says how many seconds to wait.
class Orders
{
// Returns how many seconds to wait before the next call, or 0 when the call worked.
public static function send(string $url): int
{
Core\Http\Response $response = Core\Http\Client::post($url, { json: ['id' => 1042] });
if ($response->status() != 429) {
return 0;
}
// Status 429 means "too many requests". `Retry-After` gives the seconds to wait.
// When it is missing or not a number, the program waits 60 seconds.
?tainted string $wait = $response->header('Retry-After');
return ($wait as ?int) ?? 60;
}
}
// Two fixed replies, so the example runs without a network.
Core\Test::answerHttp('https://api.example.com/orders', 201, {});
Core\Test::answerHttp('https://busy.example.com/orders', 429, { headers: ['Retry-After' => '30'] });
// This prints `wait 0 seconds`, then `wait 30 seconds`.
echo 'wait ', Orders::send('https://api.example.com/orders'), ' seconds', "\n";
echo 'wait ', Orders::send('https://busy.example.com/orders'), ' seconds', "\n";
Output
wait 0 seconds
wait 30 seconds