zendframework/zend-http
Zend\Http is a PHP HTTP component for building and processing HTTP messages. It provides clients and adapters for making requests, plus request/response objects, headers, cookies, and utilities to work with URIs and HTTP-level features in your apps.
Zend\Http\Headers is a container for HTTP headers. It is typically accessed as
part of a Zend\Http\Request or Zend\Http\Response instance, via a
getHeaders() call. The Headers container will lazily load actual
Zend\Http\Header\HeaderInterface instances as to reduce the overhead of header
specific parsing.
The class under the Zend\Http\Header namespace are the domain specific
implementations for the various types of headers that one might encounter during
the typical HTTP request. If a header of unknown type is encountered, it will be
implemented as a Zend\Http\Header\GenericHeader instance. See the below table
for a list of the various HTTP headers and the API that is specific to each
header type.
The quickest way to get started interacting with header objects is by retrieving
an already populated Headers container from a request or response instance.
// $client is an instance of Zend\Http\Client
// You can retrieve the request headers by first retrieving
// the Request object and then calling getHeaders on it
$requestHeaders = $client->getRequest()->getHeaders();
// The same method also works for retrieving Response headers
$responseHeaders = $client->getResponse()->getHeaders();
Zend\Http\Headers can also extract headers from a string:
use Zend\Http\Headers;
$headerString = <<<EOB
Host: www.example.com
Content-Type: text/html
Content-Length: 1337
EOB;
$headers = Headers::fromString($headerString);
// $headers is now populated with three objects
// (1) Zend\Http\Header\Host
// (2) Zend\Http\Header\ContentType
// (3) Zend\Http\Header\ContentLength
Now that you have an instance of Zend\Http\Headers, you can manipulate the
individual headers it contains using the provided public API methods outlined in
the Available Methods section.
No configuration options are available.
The following is a list of methods available to Zend\Http\Headers. For
brevity, we map the following references to the following classes or namespaces:
HeaderInterface: Zend\Http\Header\HeaderInterfaceHeaders: Zend\Http\HeadersPluginClassLocator: Zend\Loader\PluginClassLocator| Method signature | Description |
|---|---|
static fromString(string $string) : Headers |
Parses a string for headers, and aggregates them, in order, a new Headers instance, primarily as strings until they are needed (they will be lazy loaded). |
setPluginClassLoader(PluginClassLocator $pluginClassLoader) : self |
Set an alternate implementation for the plugin class loader. |
getPluginClassLoader() : PluginClassLocator |
Return an instance of a PluginClassLocator; lazy-load and inject map if necessary. |
| `addHeaders(array | Traversable $headers) : self` |
addHeaderLine(string $headerFieldNameOrLine, string $fieldValue) : self |
Add a raw header line, either as separate name and value arguments, or as a single string in the form name: value This method allows for lazy-loading in that the parsing and instantiation of a HeaderInterface implementation will be delayed until they are retrieved by either get() or current(). |
addHeader(HeaderInterface $header) : self |
Add a header instance to the container; for raw values see addHeaderLine() and addHeaders(). |
removeHeader(HeaderInterface $header) : bool |
Remove a Header from the container. |
clearHeaders() : self |
Removes all headers from the container. |
| `get(string $name) : false | HeaderInterface |
has(string $name) : bool |
Test for existence of a header. |
next() : void |
Advance the pointer for this object as an iterator. |
key() : mixed |
Return the current key for this object as an iterator. |
valid() : bool |
Is this iterator still valid? |
rewind() : void |
Reset the internal pointer for this object as an iterator. |
current() : HeaderInterface |
Return the current value for this iterator, lazy loading it if need be. |
count() : int |
Return the number of headers in this container. If all headers have not been parsed, the actual count could decrease if MultipleHeader instances exist. If you need an exact count, iterate. |
toString() : string |
Render all headers at once. This method handles the normal iteration of headers; it is up to the concrete classes to prepend with the appropriate status/request line. |
toArray() : array |
Return all headers as an associative array. |
forceLoading() : bool |
By calling this, it will force parsing and loading of all headers, ensuring count() is accurate. |
The following are methods available to all HeaderInterface implementations.
| Method signature | Description |
|---|---|
static fromString(string $headerLine) : HeaderInterface |
Factory to generate a header object from a string. |
getFieldName() : string |
Retrieve header field name. |
getFieldValue() : string |
Retrieve header field value. |
toString() : string |
Cast the header to a well-formed HTTP header line (Name: Value). |
Zend\Http\Header\AbstractAccept defines the following methods in addition to
those it inherits from the HeaderInterface. The
Accept, AcceptCharset, AcceptEncoding, and AcceptLanguage header types
inherit from it.
For brevity, we map the following references to the following classes or namespaces:
AcceptFieldValuePart: Zend\Http\Header\Accept\FieldValuePart\AcceptFieldValuePartInvalidArgumentException: Zend\Http\Header\Exception\InvalidArgumentException| Method signature | Description |
|---|---|
parseHeaderLine(string $headerLine) : void |
Parse the given header line and add the values discovered to the instance. |
getFieldValuePartsFromHeaderLine(string $headerLine) : array |
Parse the field value parts represented by an Accept* header line. Throws InvalidArgumentException if the header is invalid. |
| `getFieldValue(array | null $values = null) : string` |
| `match(array | string $matchAgainst) : bool |
getPrioritized() : array |
Returns all the keys, values and parameters this header represents. |
Zend\Http\Header\AbstractDate defines the following methods in addition to
those it inherits from the HeaderInterface. The
Date, Expires, IfModifiedSince, IfUnmodifiedSince, LastModified, and
RetryAfter header types inherit from it.
For brevity, we map the following references to the following classes or namespaces:
InvalidArgumentException: Zend\Http\Header\Exception\InvalidArgumentException| Method signature | Description |
|---|---|
static fromTimestamp(int $time) : AbstractDate |
Create date-based header from Unix timestamp. |
static fromTimeString(string $time) : AbstractDate |
Create date-based header from strtotime()-compatible string. |
static setDateFormat(int $format) : void |
Set date output format; should be an index from the implementation's $dateFormat static property. |
static getDateFormat() : string |
Return current date output format. |
| `setDate(string | DateTime $date) : self` |
getDate() : string |
Return string representation of the date for this header. |
| `compareTo(string | DateTime $date) : int` |
| `date() | DateTime` |
Zend\Http\Header\AbstractLocation defines the following methods in addition to
those it inherits from the HeaderInterface. The
ContentLocation, Location, and Referer header types inherit from it.
For brevity, we map the following references to the following classes or namespaces:
Uri: Zend\Uri\UriInterfaceInvalidArgumentException: Zend\Http\Header\Exception\InvalidArgumentException| Method signature | Description |
|---|---|
| `setUri(string | Uri $uri) : self` |
getUri() : string |
Return the URI for this header. |
uri() : Uri |
Return the Uri instance for this header. |
Some header classes expose methods for manipulating their value. The following
list contains all of the classes available in the Zend\Http\Header\*
namespace, as well as any specific methods they contain. Each class implements
Zend\Http\Header\HeaderInterface.
Extends AbstractAccept.
| Method signature | Description |
|---|---|
| `addMediaType(string $type, int | float $priority = 1) : self` |
hasMediaType(string $type): bool |
Does the header have the requested media type? |
Extends AbstractAccept.
| Method signature | Description |
|---|---|
| `addCharset(string $type, int | float $priority = 1) : self` |
hasCharset(string $type) : bool |
Does the header have the requested charset? |
Extends AbstractAccept.
| Method signature | Description |
|---|---|
| `addEncoding(string $type, int | float $priority = 1) : self` |
hasEncoding(string $type) : bool |
Does the header have the requested encoding? |
Extends AbstractAccept.
| Method signature | Description |
|---|---|
| `addLanguage(string $type, int | float $priority = 1): self` |
hasLanguage(string $type) : bool |
Does the header have the requested language? |
| Method signature | Description |
|---|---|
getRangeUnit() : mixed |
(unknown) |
setRangeUnit(mixed $rangeUnit) : self |
(unkown) |
| Method signature | Description |
|---|---|
getDeltaSeconds() : int |
Get number of seconds. |
setDeltaSeconds(int $seconds) : self |
Set number of seconds |
| Method signature | Description |
|---|---|
getAllMethods() : string[] |
Get list of all defined methods. |
getAllowedMethods() : string[] |
Get list of allowed methods. |
| `allowMethods(array | string $allowedMethods) : self` |
| `disallowMethods(array | string $allowedMethods) self` |
| `denyMethods(array | string $allowedMethods) : self` |
isAllowedMethod(string $method) : bool |
Check whether method is allowed. |
No additional methods.
No additional methods.
| Method signature | Description |
|---|---|
isEmpty(): bool |
Checks if the internal directives array is empty. |
| `addDirective(string $key, string | bool $value = true) : self` |
hasDirective(string $key) : bool |
Check the internal directives array for a directive. |
| `getDirective(string $key) : null | string` |
removeDirective(string $key) : self |
Remove a directive. |
| Method signature | Description |
|---|---|
setValue($value) : self |
Set arbitrary header value. RFC allows any token as value; 'close' and 'keep-alive' are commonly used. |
isPersistent() : bool |
Whether or not the connection is persistent. |
setPersistent(bool $flag) : self |
Set Connection header to define persistent connection. |
No additional methods.
No additional methods.
No additional methods.
No additional methods.
See AbstractLocation.
No additional methods.
No additional methods.
| Method signature | Description |
|---|---|
getDirectives(): array |
Retrieve the defined directives for the policy. |
setDirective(string $name, array $sources) : self |
Set the directive with the given name to include the sources. See below for an example. |
As an example: an auction site wishes to load images from any URI, plugin content from a list of trusted media providers (including a content distribution network), and scripts only from a server under its control hosting sanitized Javacript:
// http://www.w3.org/TR/2012/CR-CSP-20121115/#sample-policy-definitions
$csp = new ContentSecurityPolicy();
$csp->setDirective('default-src', []); // No sources
$csp->setDirective('img-src', ['*']);
$csp->setDirective('object-src', ['media1.example.com', 'media2.example.com', '*.cdn.example.com']);
$csp->setDirective('script-src', ['trustedscripts.example.com']);
No additional methods.
| Method signature | Description |
|---|---|
| `match(array | string $matchAgainst) : bool |
getMediaType() : string |
Get the media type. |
setMediaType(string $mediaType) : self |
Set the media type. |
getParameters() : array |
Get any additional content-type parameters currently set. |
setParameters(array $parameters) : self |
Set additional content-type parameters. |
| `getCharset() : null | string` |
setCharset(string $charset) : self |
Set the content-type character set encoding. |
Extends ArrayObject.
| Method signature | Description |
|---|---|
static fromSetCookieArray(array $setCookies) : Cookie |
Create an instance from the $_COOKIE array, or one structured like it. |
setEncodeValue(bool $encode) : self |
Set flag indicating whether or not to urlencode() the cookie values. |
getEncodeValue() : bool |
Get flag indicating whether or not to urlencode() the cookie values. |
See AbstractDate.
No additional methods.
No additional methods.
See AbstractDate.
No additional methods.
No additional methods.
No additional methods.
See AbstractDate.
No additional methods.
No additional methods.
See AbstractDate.
No additional methods.
See AbstractDate.
See AbstractLocation.
No additional methods.
No additional methods.
No additional methods.
| Method signature | Description |
|---|---|
toStringMultipleHeaders(array $headers) : string |
Creates a string representation when multiple values are present. |
No additional methods.
No additional methods.
See AbstractLocation.
No additional methods.
See AbstractDate.
| Method signature | Description |
|---|---|
setDeltaSeconds(int $delta) : self |
Set number of seconds. |
getDeltaSeconds() : int |
Get number of seconds. |
No additional methods.
| Method signature | Description |
|---|---|
static matchCookieDomain(string $cookieDomain, string $host) : bool |
Check if a cookie's domain matches a host name. |
static matchCookiePath(string $cookiePath, string $path) : bool |
Check if a cookie's path matches a URL path. |
getName() : string |
Retrieve the cookie name. |
setName(string $name) : self |
Set the cookie name. |
getValue() : string |
Retrieve the cookie value. |
setValue(string $value) : self |
Set the cookie value. |
getExpires() : int |
Retrieve the expiration date for the cookie. |
| `setExpires(int | string $expires) : self` |
getPath() : string |
Retrieve the URI path the cookie is bound to. |
setPath(string $path) : self |
Set the URI path the cookie is bound to. |
getDomain() : string |
Retrieve the domain the cookie is bound to. |
setDomain(string $domain) : self |
Set the domain the cookie is bound to. |
getMaxAge() : int |
Retrieve the maximum age for the cookie. |
| `setMaxAge(int | string $maxAge) : self` |
getVersion() : int |
Retrieve the cookie version. |
| `setVersion(int | string $version) : self` |
isSecure(): bool |
Whether the cookies contains the Secure flag. |
setSecure(bool $secure) : self |
Set whether the cookies contain the Secure flag. |
isHttponly() : bool |
Whether the cookies can be accessed via the HTTP protocol only. |
setHttponly(bool $httponly) : self |
Set whether the cookies can be accessed only via HTTP protocol. |
getSameSite() : ?string |
Retrieve the SameSite directive value (null when it is not set). |
| `setSameSite(?string $same... |
How can I help you explore Laravel packages today?