Retrieving Headers
Headers are retrieved through the HeaderSet's get() and getAll() methods.
To get a header, or several headers from a MIME entity:
- Get the HeaderSet from the entity by via its getHeaders() method.
- Get the header(s) from the HeaderSet by calling either get() or getAll().
When using get() a single header is returned that matches the name (case insensitive) that is passed to it. When using getAll() with a header name, an array of headers with that name are returned. Calling getAll() with no arguments returns an array of all headers present in the entity.
Note:
It's valid for some headers to appear more than once in a message (e.g. the Received header).
For this reason getAll() exists to fetch all headers with a specified name.
In addition, get() accepts an optional numerical index, starting from zero
to specify which header you want more specifically.
Note:
If you want to modify the contents of the header and you don't know for sure what type of
header it is (see Header Types) then you may need to
check the type by calling its getFieldType() method.
$headers = $message->getHeaders();
//Get the To: header
$toHeader = $headers->get('To');
//Get all headers named "X-Foo"
$fooHeaders = $headers->getAll('X-Foo');
//Get the second header named "X-Foo"
$foo = $headers->get('X-Foo', 1);
//Get all headers that are present
$all = $headers->getAll();
Parent topic: Header Operations
Previous topic: Adding new Headers
Next topic: Check if a Header Exists