HTTPHeader is a PHP class for inspecting HTTP headers.
- PHP 8.0+
Methods for inspecting response headers must be supplied with a string containing one or more headers, or a complete HTTP response.
Examples:
use xenocrat\HTTPHeader;
$result = HTTPHeader::Server("Server: Apache");
$result = HTTPHeader::Server("Content-Type: text/plain\r\nServer: Apache");
$result = HTTPHeader::Server("HTTP/1.1 200 OK\r\nServer: Apache\r\n\r\nHello, world!");
Methods for inspecting request headers can optionally be supplied with a string containing one or more headers, or a complete HTTP request; if not supplied with a string, these methods will attempt to read the value from the $_SERVER
superglobal.
Examples:
use xenocrat\HTTPHeader;
$result = HTTPHeader::Accept();
$result = HTTPHeader::Accept("Accept: text/html, application/xhtml+xml");
$result = HTTPHeader::Accept("Accept: text/html\r\nAccept-Encoding: gzip");
All fields can be extracted from a string containing multiple headers, or from a complete HTTP request or response, using the extract()
method. The return value is an associative array of arrays containing one entry for each occurence of a field:
Example:
$result = HTTPHeader::extract($request_or_response);
print_r($result);
Array
(
[CONTENT_TYPE] => Array
(
[0] => Array
(
[type] => text/plain
)
)
[SERVER] => Array
(
[0] => Array
(
[0] => Array
(
[product] => Apache
)
)
)
)
Methods will return false
if the header field is not present or empty, and null
if the field value is noticeably malformed.
Returns an array sorted by "q" value.
Example:
Array
(
[0] => text/html
[1] => application/xhtml+xml
[2] => image/webp
[3] => application/xml;q=0.9
[4] => */*;q=0.8
)
Returns an array of client hint headers.
Returns an array sorted by "q" value.
Returns an array sorted by "q" value.
Example:
Array
(
[0] => gzip
[1] => deflate
)
Returns an array sorted by "q" value.
Example:
Array
(
[0] => en-GB
[1] => en;q=0.7
[2] => en-US;q=0.3
)
Returns an array of media types.
Example:
Array
(
[0] => application/example
[1] => text/example;charset=utf-8
)
Returns an array of media types.
Example:
Array
(
[0] => image/webp
[1] => text/example;charset=utf-8
)
Returns a range unit.
Returns true
if the value is "true", or null
otherwise.
Returns an array of headers.
Example:
Array
(
[0] => Accept
[1] => Upgrade-Insecure-Requests
)
Returns an array of method names.
Example:
Array
(
[0] => POST
[1] => GET
[2] => OPTIONS
)
Returns the string "*"
, the string "null"
if the value is "null", or the result of parse_url()
on the supplied value.
Returns an array of headers.
Example:
Array
(
[0] => Content-Encoding
)
Returns a non-negative integer representing the number of seconds.
Returns an array of headers.
Example:
Array
(
[0] => Content-Type
)
Returns the method name.
Returns a non-negative integer representing a time delta in seconds.
Returns an array of method names.
Example:
Array
(
[0] => GET
[1] => POST
[2] => HEAD
)
Returns the string "clear"
, or an array of associative arrays containing the parameters for each alternative service.
Example:
Array
(
[0] => Array
(
[protocol] => h3-25
[host] =>
[port] => 443
[ma] => 3600
[persist] => <true|false>
)
[1] => Array
(
[protocol] => h2
[host] => example.com
[port] => 443
[ma] => 3600
[persist] => <true|false>
)
)
Returns an associative array containing the host, and port if supplied.
Example:
Array
(
[host] => example.com
[port] => 80
)
Returns an array containing the authorization type and parameters.
Example:
Array
(
[0] => Basic
[1] => YWxhZGRpbjpvcGVuc2VzYW1l
)
Returns an array of directives.
Example:
Array
(
[0] => only-if-cached
)
Returns an array of directives.
Example:
Array
(
[0] => storage
[1] => cookies
)
Returns an array of directives.
Example:
Array
(
[0] => keep-alive
)
Returns an associative array containing the content disposition, field name and filename (if supplied).
Example:
Array
(
[disposition] => attachment
[filename] => filename.jpg
)
Returns an array of encoding formats in the order in which they were applied.
Example:
Array
(
[0] => deflate
[1] => gzip
)
Returns an array of language tags.
Example:
Array
(
[0] => en-GB
[1] => en
)
Returns the content length in decimal number of octets.
Returns a string.
Returns an associative array containing the unit, range, and size.
Example:
Array
(
[unit] => bytes
[range] => 200-1000
[size] => 67589
)
Returns an array of arrays containing the policy directive and values.
Example:
Array
(
[0] => Array
(
[0] => default-src
[1] => Array
(
[0] => self
[1] => nonce-DhcnhD3khTMePgXwdayK9BsMqXjhguVV
)
)
[1] => Array
(
[0] => form-action
[1] => Array
(
[0] => self
)
)
See above.
Returns an associative array containing the content type, charset and boundary (if supplied).
Example:
Array
(
[type] => multipart/form-data
[boundary] => something
)
Returns an array of arrays containing the cookie names and value.
Example:
Array
(
[0] => Array
(
[0] => Session
[1] => 0ae5ab57925bcbee58917d552acb4cd4
)
)
Returns a policy directive.
Returns a policy directive.
Returns a policy directive.
Returns a DateTimeImmutable object.
Example:
DateTimeImmutable Object
(
[date] => 2015-10-21 07:28:00.000000
[timezone_type] => 2
[timezone] => GMT
)
Returns a non-negative float representing the device memory in GiB.
Returns an array of arrays containing the digest algorithms and values.
Example:
Array
(
[0] => Array
(
[0] => sha-256
[1] => X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=
)
[1] => Array
(
[0] => unixsum
[1] => 30637
)
)
Returns a non-negative float representing the downlink rate in Mbps.
Returns the integer 0
, 1
, or null
if the value is indeterminate.
Returns a string identifying the effective connection type, or null
if the value is indeterminate.
Returns an ETag value.
Returns the integer 100
if the value is "100-continue", or null
otherwise.
Returns a DateTimeImmutable object.
Example:
DateTimeImmutable Object
(
[date] => 2015-10-21 07:28:00.000000
[timezone_type] => 2
[timezone] => GMT
)
Returns an array of fields, each containing an associative array of directives.
Example:
Array
(
[0] => Array
(
[for] => 192.0.2.60
[proto] => http
[by] => 203.0.113.43
)
[1] => Array
(
[for] => 198.51.100.17
)
)
Returns a string containing the supplied email address.
Returns an associative array containing the host, and port if supplied.
Example:
Array
(
[host] => example.com
[port] => 80
)
Returns an array of ETag values.
Example:
Array
(
[0] => W/"67ab43"
[1] => "54ed21"
[2] => "7892dd"
)
Returns a DateTimeImmutable object.
Example:
DateTimeImmutable Object
(
[date] => 2015-10-21 07:28:00.000000
[timezone_type] => 2
[timezone] => GMT
)
Returns an array of ETag values.
Example:
Array
(
[0] => W/"67ab43"
[1] => "54ed21"
[2] => "7892dd"
)
Returns a DateTimeImmutable object, or an array of ETag values.
Returns a DateTimeImmutable object.
Example:
DateTimeImmutable Object
(
[date] => 2015-10-21 07:28:00.000000
[timezone_type] => 2
[timezone] => GMT
)
Returns an associative array of parameters.
Example:
Array
(
[timeout] => 5
[max] => 1000
)
Returns a DateTimeImmutable object.
Example:
DateTimeImmutable Object
(
[date] => 2015-10-21 07:28:00.000000
[timezone_type] => 2
[timezone] => GMT
)
Returns an array of arrays containing the link URI and an array of parameters.
Example:
Array
(
[0] => Array
(
[0] => https://one.example.com
[1] => Array
(
[0] => Array
(
[0] => rel
[1] => preconnect
)
)
)
[1] => Array
(
[0] => https://two.example.com
[1] => Array
(
[0] => Array
(
[0] => rel
[1] => preconnect
)
[1] => Array
(
[0] => foo
[1] => bar
)
)
)
)
Returns a string.
Returns a non-negative integer.
Returns the result of parse_url()
on the supplied value.
Example:
Array
(
[scheme] => https
[host] => example.com
)
Returns an array of arrays containing the policy directive and values.
Example:
Array
(
[0] => Array
(
[0] => picture-in-picture
[1] => Array
(
)
)
[1] => Array
(
[0] => geolocation
[1] => Array
(
[0] => self
[1] => https://example.com
)
)
[2] => Array
(
[0] => camera
[1] => Array
(
[0] => *
)
)
)
Returns an array of directives.
Returns an array of arrays containing the authentication type and parameters.
Example:
Array
(
[0] => Array
(
[0] => Basic
[1] => realm="foo"
)
[1] => Array
(
[0] => Other
[1] => realm="bar"
)
)
Returns an array containing the authorization type and parameters.
Example:
Array
(
[0] => Basic
[1] => YWxhZGRpbjpvcGVuc2VzYW1l
)
Returns an associative array containing the unit and ranges.
Example:
Array
(
[unit] => bytes
[ranges] => Array
(
[0] => 200-1000
[1] => 2000-6576
[2] => 19000-
)
)
Returns the result of parse_url()
on the supplied value.
Example:
Array
(
[scheme] => https
[host] => example.com
[path] => /foo/
)
Returns a directive.
Returns a DateTimeImmutable object, or a non-negative integer representing the delay in seconds.
Returns a non-negative integer representing the approximate round trip time in milliseconds.
Returns the integer 0
for falsey values, 1
for truthy values, or null
if the value is indeterminate.
Returns a directive.
Returns a directive.
Returns a directive.
Returns true
if the value is "?1", or null
otherwise.
Returns true
if the value is "1", or null
otherwise.
Returns a directive.
Returns an array of associative arrays containing the product, version and comment (if supplied).
Example:
Array
(
[0] => Array
(
[product] => Apache
[version] => 2.4.1
[comment] => Unix
)
)
Returns an array of associative arrays containing the metric name, description and duration (if supplied).
Example:
Array
(
[0] => Array
(
[name] => db
[dur] => 53
)
[1] => Array
(
[name] => cache
[desc] => Cache Read
[dur] => 23.2
)
)
Returns a string.
Returns an array of arrays containing the cookie name and value, and an associative array of parameter values.
Example:
Array
(
[0] => Array
(
[0] => Session
[1] => 0ae5ab57925bcbee58917d552acb4cd4
)
[1] => Array
(
[Path] => /
[Domain] => example.com
[SameSite] =>
[Expires] => DateTimeImmutable Object
(
[date] => 2015-10-21 07:28:00.000000
[timezone_type] => 2
[timezone] => GMT
)
[Max-Age] => 20
[HttpOnly] => <true|false>
[Secure] => <true|false>
[Partitioned] => <true|false>
)
)
Returns a string.
Returns an associative array of parameter values.
Example:
Array
(
[max-age] => 63072000
[includeSubDomains] => <true|false>
[preload] => <true|false>
)
Returns an array sorted by "q" value.
Example:
Array
(
[0] => trailers
[1] => gzip
[2] => deflate;q=0.5
)
Returns the string "*"
, or an array containing the results of parse_url()
on each of the supplied values.
Example:
Array
(
[0] => Array
(
[scheme] => https
[host] => mozilla.org
)
[1] => Array
(
[scheme] => http
[host] => example.com
)
)
Returns an array of field names.
Example:
Array
(
[0] => Expires
[1] => Date
)
Returns an array of encoding formats in the order in which they were applied.
Example:
Array
(
[0] => gzip
[1] => chunked
)
Returns an array of protocols in order of preference.
Example:
Array
(
[0] => HTTP/2
[1] => HTTP/1.1
)
Returns the integer 0
, 1
, or null
if the value is indeterminate.
Returns an array of associative arrays containing the product, version and comment (if supplied).
Example:
Array
(
[0] => Array
(
[product] => Mozilla
[version] => 5.0
[comment] => Windows NT 10.0; Win64; x64; rv:109.0
)
[1] => Array
(
[product] => Gecko
[version] => 20100101
)
[2] => Array
(
[product] => Firefox
[version] => 115.0
)
)
Returns the string "*"
, or an array of field names.
Example:
Array
(
[0] => Cookie
[1] => Save-Data
)
Returns an array of associative arrays containing the details supplied by each proxy.
Example:
Array
(
[0] => Array
(
[protocol] => HTTP
[version] => 1.0
[pseudonym] => foo
)
[1] => Array
(
[version] => 1.1
[pseudonym] => bar
)
[2] => Array
(
[version] => 1.1
[pseudonym] => example.com
[port] => 80
[comment] => this is a comment
)
)
Returns an array sorted by "q" value.
Example:
Array
(
[0] => sha-256;q=1
[1] => SHA-512;q=0.3
[2] => md5;q=0
)
Returns an array of arrays containing the authentication type and parameters.
Example:
Array
(
[0] => Array
(
[0] => scheme1
[3] => realm="foo"
[4] => param1=token1
[5] => param2=token2
)
[1] => Array
(
[0] => scheme2
[1] => token68
[2] => realm="bar"
)
[2] => Array
(
[0] => scheme3
)
)
Returns the string "nosniff"
, or null
if the value is indeterminate.
Returns a directive.