A Zend\Http\Cookies object is designed to contain and maintain HTTP cookies, and should
be used along with Zend\Http\Client in order to manage cookies across HTTP requests and
responses.
The class contains an array of Zend\Http\Header\Cookie objects. Cookies can be added
automatically from a request or manually. Then, the Cookies class can find and return the
cookies needed for a specific HTTP request.
A special parameter can be passed to all methods of this class that return cookies: Cookies
can be returned either in their native form (as Zend\Http\Header\Cookie objects) or as strings -
the later is suitable for sending as the value of the "Cookie" header in an HTTP request.
You can also choose, when returning more than one cookie, whether to get an array of strings
(by passing Zend\Http\Client\Cookies::COOKIE_STRING_ARRAY) or one unified string for all cookies
(by passing Zend\Http\Client\Cookies::COOKIE_STRING_CONCAT).
Methods
Add a cookie to the class.
addCookie(\Zend\Http\Header\SetCookie | string $cookie, \Zend\Uri\Uri | string $refUri)
Cookie should be passed either as a Zend\Http\Header\Cookie object
or as a string - in which case an object is created from the string.
Parameters
$refUri
\Zend\Uri\Uristring
Optional reference URI (for domain, path, secure)
Exceptions
Parse an HTTP response, adding all the cookies set in that response
addCookiesFromResponse(\Zend\Http\Response $response, \Zend\Uri\Uri | string $refUri)
Return the number of headers in this contain, if all headers have not been parsed, actual count could
increase if MultipleHeader objects exist in the Request/Response.
count() : int
Inherited
If you need an exact count, iterate
inherited_from |
\Zend\Http\Headers::count() |
Returns
int
count of currently known headers
Return the current value for this iterator, lazy loading it if need be
current() : array | \Zend\Http\Header\HeaderInterface
Inherited
inherited_from |
\Zend\Http\Headers::current() |
Returns
By calling this, it will force parsing and loading of all headers, after this count() will be accurate
forceLoading() : bool
Inherited
inherited_from |
\Zend\Http\Headers::forceLoading() |
Returns
bool
Create a new Cookies object and automatically load into it all the
cookies set in a Response object.
fromResponse(\Zend\Http\Response $response, \Zend\Uri\Uri | string $refUri) : \Zend\Http\Cookies
If $uri is set, it will be
considered as the requested URI for setting default domain and path
of the cookie.
todo |
Add the $uri functionality. |
Parameters
Returns
Populates headers from string representation
fromString($string) : void
Parses a string for headers, and aggregates them, in order, in the
current instance, primarily as strings until they are needed (they
will be lazy loaded)
Parameters
$string
Exceptions
Get all cookies in the cookie jar as an array
getAllCookies(int $retAs) : array | string
Parameters
$retAs
int
Whether to return cookies as objects of \Zend\Http\Header\SetCookie or as strings
Returns
array
string
Get a specific cookie according to a URI and name
getCookie(\Zend\Uri\Uri | string $uri, string $cookieName, int $retAs) : \Zend\Http\Header\SetCookie | string
Parameters
$cookieName
string
The cookie's name
$retAs
int
Whether to return cookies as objects of \Zend\Http\Header\SetCookie or as strings
Exceptions
Returns
Return an array of all cookies matching a specific request according to the request URI,
whether session cookies should be sent or not, and the time to consider as "now" when
checking cookie expiry time.
getMatchingCookies(string | \Zend\Uri\Uri $uri, bool $matchSessionCookies, int $retAs, int $now) : array | string
Parameters
$uri
string\Zend\Uri\Uri
URI to check against (secure, domain, path)
$matchSessionCookies
bool
Whether to send session cookies
$retAs
int
Whether to return cookies as objects of \Zend\Http\Header\Cookie or as strings
$now
int
Override the current time when checking for expiry time
Exceptions
Returns
array
string
Return an instance of a PluginClassLocator, lazyload and inject map if necessary
getPluginClassLoader() : \Zend\Loader\PluginClassLocator
Inherited
inherited_from |
\Zend\Http\Headers::getPluginClassLoader() |
Returns
Test for existence of a type of header
has(string $name) : bool
Inherited
inherited_from |
\Zend\Http\Headers::has() |
Parameters
$name
string
Returns
bool
Tells if the array of cookies is empty
isEmpty() : bool
Return the current key for this object as an iterator
key() : mixed
Inherited
inherited_from |
\Zend\Http\Headers::key() |
Returns
mixed
Advance the pointer for this object as an iterator
next() : void
Inherited
inherited_from |
\Zend\Http\Headers::next() |
Reset the internal pointer for this object as an iterator
rewind() : void
Inherited
inherited_from |
\Zend\Http\Headers::rewind() |
Set an alternate implementation for the PluginClassLoader
setPluginClassLoader(\Zend\Loader\PluginClassLocator $pluginClassLoader) : \Zend\Http\Headers
Inherited
inherited_from |
\Zend\Http\Headers::setPluginClassLoader() |
Parameters
Returns
Return the headers container as an array
toArray() : array
Inherited
inherited_from |
\Zend\Http\Headers::toArray() |
Returns
array
Render all headers at once
toString() : string
Inherited
This method handles the normal iteration of headers; it is up to the
concrete classes to prepend with the appropriate status/request line.
inherited_from |
\Zend\Http\Headers::toString() |
Returns
string
Is this iterator still valid?
valid() : bool
Inherited
inherited_from |
\Zend\Http\Headers::valid() |
Returns
bool
Helper function to recursively flatten an array.
_flattenCookiesArray(\Zend\Http\Header\SetCookie | array $ptr, int $retAs) : array | string
Should be used when exporting the
cookies array (or parts of it)
Parameters
$retAs
int
What value to return
Returns
array
string
Return a subset of the cookies array matching a specific domain
_matchDomain(string $domain) : array
Parameters
$domain
string
Returns
array
Return a subset of a domain-matching cookies that also match a specified path
_matchPath(array $domains, string $path) : array
Parameters
$domains
array
$path
string
Returns
array
Create array key from header name
createKey(string $name) : string
Inherited
inherited_from |
\Zend\Http\Headers::createKey() |
Parameters
$name
string
Returns
string
Properties
$cookies
$cookies : array
$rawCookies
$rawCookies : array
Constants
Return cookie(s) as a Zend\Http\Cookie object
COOKIE_OBJECT
Return cookie(s) as a string (suitable for sending in an HTTP request)
COOKIE_STRING_ARRAY
Return all cookies as one long string (suitable for sending in an HTTP request)
COOKIE_STRING_CONCAT
Return all cookies as one long string (strict mode)
- Single space after the semi-colon separating each cookie
- Remove trailing semi-colon, if any
COOKIE_STRING_CONCAT_STRICT