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).

link for some specs.

 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

\Zend\Http\Exception\InvalidArgumentException

Parse an HTTP response, adding all the cookies set in that response

addCookiesFromResponse(\Zend\Http\Response $response, \Zend\Uri\Uri | string $refUri) 

Parameters

$refUri

\Zend\Uri\Uristring

Requested URI

Add a Header to this container, for raw values @see addHeaderLine() and addHeaders()

addHeader(\Zend\Http\Header\HeaderInterface $header) : \Zend\Http\Headers
Inherited

inherited_from \Zend\Http\Headers::addHeader()

Parameters

Returns

Add a raw header line, either in name => value, or as a single string 'name: value'

addHeaderLine(string $headerFieldNameOrLine, string $fieldValue) : \Zend\Http\Headers
Inherited

This method allows for lazy-loading in that the parsing and instantiation of Header object will be delayed until they are retrieved by either get() or current()

inherited_from \Zend\Http\Headers::addHeaderLine()

Parameters

$headerFieldNameOrLine

string

$fieldValue

string

optional

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

Add many headers at once

addHeaders(array | \Traversable $headers) : \Zend\Http\Headers
Inherited

Expects an array (or Traversable object) of type/value pairs.

inherited_from \Zend\Http\Headers::addHeaders()

Parameters

$headers

array\Traversable

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

Clear all headers

clearHeaders() : \Zend\Http\Headers
Inherited

Removes all headers from queue

inherited_from \Zend\Http\Headers::clearHeaders()

Returns

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

intcount 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 an Http_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

$response

\Zend\Http\Response

HTTP Response object

$refUri

\Zend\Uri\Uristring

The requested URI

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)

static

Parameters

$string

Exceptions

\Zend\Http\Exception\RuntimeException

Get all headers of a certain name/type

get(string $name) : bool | \Zend\Http\Header\HeaderInterface | \ArrayIterator
Inherited

inherited_from \Zend\Http\Headers::get()

Parameters

$name

string

Returns

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

arraystring

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

$uri

\Zend\Uri\Uristring

The uri (domain and path) to match

$cookieName

string

The cookie's name

$retAs

int

Whether to return cookies as objects of \Zend\Http\Header\SetCookie or as strings

Exceptions

\Zend\Http\Exception\InvalidArgumentException if invalid URI specified or invalid $retAs value

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

\Zend\Http\Exception\InvalidArgumentException if invalid URI specified

Returns

arraystring

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

Returns

bool

Return the current key for this object as an iterator

key() : mixed
Inherited

inherited_from \Zend\Http\Headers::key()

Returns

mixed

Remove a Header from the container

removeHeader(\Zend\Http\Header\HeaderInterface $header) : bool
Inherited

inherited_from \Zend\Http\Headers::removeHeader()

Parameters

Returns

bool

Empties the cookieJar of any cookie

reset() : \Zend\Http\Cookies

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

$pluginClassLoader

\Zend\Loader\PluginClassLocator

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

arraystring

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

lazyLoadHeader()

lazyLoadHeader($index) : mixed | void
Inherited

inherited_from \Zend\Http\Headers::lazyLoadHeader()

Parameters

$index

Returns

mixedvoid

 Properties

 

$cookies

$cookies : \Zend\Http\Cookies

 

$headers

$headers : \Zend\Http\Headers

 

<p>key names for $headers array</p>

$headersKeys : array
Inherited

inherited_from \Zend\Http\Headers::$$headersKeys
 

$pluginClassLoader

$pluginClassLoader : \Zend\Http\PluginClassLoader
Inherited

inherited_from \Zend\Http\Headers::$$pluginClassLoader
 

$rawCookies

$rawCookies : array

 Constants