Working with headers
Headers are the metadata around an HTTP message. They describe things such as:
- what content type is being sent
- which formats the client accepts
- how caching should behave
- whether cookies should be set
This package provides header helpers at two levels:
GT\Http\Header\HeaderLinerepresents one named header lineGT\Http\Header\Headersrepresents a whole collection of header lines
There are also the specialised subclasses:
RequestHeadersResponseHeaders
Create a header collection
use GT\Http\Header\Headers;
$headers = new Headers([
"Content-Type" => "application/json",
"Cache-Control" => "no-store",
]);
Case-insensitive access
Header names are case-insensitive, so all of these work the same way:
$headers->contains("content-type");
$headers->contains("Content-Type");
$headers->contains("CONTENT-TYPE");
Add, replace, and remove
$headers->add("Accept", "application/json");
$headers->add("Accept", "application/xml");
$headers->set("Content-Type", "text/plain");
$headers->remove("Cache-Control");
add() appends values, while set() replaces any existing values for that header name.
Read values
$line = $headers->get("Accept");
echo $line?->getValue(); // first value
$all = $headers->getAll("Accept");
get() returns a HeaderLine or null.
getAll() returns a flat array of values.
Iteration
The collection is iterable, so you can inspect every header line:
foreach($headers as $headerLine) {
echo $headerLine->getName(), ": ", $headerLine, PHP_EOL;
}
Set-Cookie is handled specially
Most headers can be combined with commas. Set-Cookie is different: each cookie line needs to stay separate.
This collection handles that for you. When multiple Set-Cookie values are present:
getAll("Set-Cookie")returns every cookie value separatelyasArray()joins them with newlines rather than commasasArray(true)preserves them as an array
That detail matters because cookies are one of the places where a naive header implementation can lose information.
[!TIP] In WebEngine applications, the
GT\Cookie\Cookieobject is available to us for working with cookies, so there’s no need to keep track of the cookie headers ourselves.
Parsing raw headers
If you have one raw header block as a string, GT\Http\Header\Parser can extract:
- the protocol version
- the status code
- key/value pairs
use GT\Http\Header\Parser;
$raw = implode("\n", [
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Set-Cookie: a=1",
"Set-Cookie: b=2",
]);
$parser = new Parser($raw);
echo $parser->getProtocolVersion(); // 1.1
echo $parser->getStatusCode(); // 200
print_r($parser->getKeyValues());
Request and response integration
Request, ServerRequest, and Response all use the shared message API:
getHeaders()hasHeader()getHeader()getHeaderLine()withHeader()withAddedHeader()withoutHeader()
That means once you understand headers here, the same reading and writing model applies everywhere in the package.
Learn more about the protocol-level background behind the first line and the header fields in Parts of the HTTP header.