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.
package Default
inherited_from \Zend\Http\Headers

 Methods

Add a cookie to the class. 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.

addCookie(\Zend\Http\Header\SetCookie|string $cookie, \Zend\Uri\Uri|string $refUri = null

Parameters

$cookie

\Zend\Http\Header\SetCookiestring

$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

$response

\Zend\Http\Response

$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

Parameters

$header

\Zend\Http\Header\HeaderInterface

Returns

\Zend\Http\Headers

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

addHeaderLine(string $headerFieldNameOrLine, string $fieldValue = null) : \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()

Parameters

$headerFieldNameOrLine

string

$fieldValue

string

optional

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

\Zend\Http\Headers

Add many headers at once

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

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

Parameters

$headers

array\Traversable

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

\Zend\Http\Headers

Clear all headers

clearHeaders() : \Zend\Http\Headers
Inherited

Removes all headers from queue

Returns

\Zend\Http\Headers

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. If you need an exact count, iterate

count() : integer
Inherited

Returns

integercount of currently known headers

Return the current value for this iterator, lazy loading it if need be

current() : array | \Zend\Http\Header\HeaderInterface
Inherited

Returns

array\Zend\Http\Header\HeaderInterface

By calling this, it will force parsing and loading of all headers, after this count() will be accurate

forceLoading() : boolean
Inherited

Returns

boolean

Create a new Cookies object and automatically load into it all the cookies set in a Response object. If $uri is set, it will be considered as the requested URI for setting default domain and path of the cookie.

fromResponse(\Zend\Http\Response $response, \Zend\Uri\Uri|string $refUri) : \Zend\Http\Cookies
Static
todo Add the $uri functionality.

Parameters

$response

\Zend\Http\Response

HTTP Response object

$refUri

\Zend\Uri\Uristring

The requested URI

Returns

\Zend\Http\Cookies

Populates headers from string representation

fromString(string $string) : void
Static

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
inherited_from \Zend\Http\Headers::fromString()

Parameters

$string

Exceptions

\Zend\Http\Exception\RuntimeException

Populates headers from string representation

fromString(string $string) : \Zend\Http\Headers
InheritedStatic

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

string

Exceptions

\Zend\Http\Exception\RuntimeException

Returns

\Zend\Http\Headers

Get all headers of a certain name/type

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

Parameters

$name

string

Returns

boolean\Zend\Http\Header\HeaderInterface\ArrayIterator

Get all cookies in the cookie jar as an array

getAllCookies(integer $retAs = self::COOKIE_OBJECT) : array | string

Parameters

$retAs

integer

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, integer $retAs = self::COOKIE_OBJECT) : \Zend\Http\Header\SetCookie | string

Parameters

$uri

\Zend\Uri\Uristring

The uri (domain and path) to match

$cookieName

string

The cookie's name

$retAs

integer

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

\Zend\Http\Header\SetCookiestring

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, boolean $matchSessionCookies = true, integer $retAs = self::COOKIE_OBJECT, integer $now = null) : array | string

Parameters

$uri

string\Zend\Uri\Uri

URI to check against (secure, domain, path)

$matchSessionCookies

boolean

Whether to send session cookies

$retAs

integer

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

$now

integer

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

Returns

\Zend\Loader\PluginClassLocator

Test for existence of a type of header

has(string $name) : boolean
Inherited

Parameters

$name

string

Returns

boolean

Tells if the array of cookies is empty

isEmpty() : boolean

Returns

boolean

Return the current key for this object as an iterator

key() : mixed
Inherited

Returns

mixed

Advance the pointer for this object as an iterator

next() : void
Inherited

Remove a Header from the container

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

Parameters

$header

\Zend\Http\Header\HeaderInterface

Returns

boolean

Empties the cookieJar of any cookie

reset() : \Zend\Http\Cookies

Returns

\Zend\Http\Cookies

Reset the internal pointer for this object as an iterator

rewind() : void
Inherited

Set an alternate implementation for the PluginClassLoader

setPluginClassLoader(\Zend\Loader\PluginClassLocator $pluginClassLoader) : \Zend\Http\Headers
Inherited

Parameters

$pluginClassLoader

\Zend\Loader\PluginClassLocator

Returns

\Zend\Http\Headers

Return the headers container as an array

toArray() : array
Inherited
todo determine how to produce single line headers, if they are supported

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.

Returns

string

Is this iterator still valid?

valid() : boolean
Inherited

Returns

boolean

Helper function to recursively flatten an array. Should be used when exporting the cookies array (or parts of it)

_flattenCookiesArray(\Zend\Http\Header\SetCookie|array $ptr, integer $retAs = self::COOKIE_OBJECT) : array | string

Parameters

$ptr

\Zend\Http\Header\SetCookiearray

$retAs

integer

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
InheritedStatic

Parameters

$name

string

Returns

string

lazyLoadHeader()

lazyLoadHeader($index) : mixed | void
Inherited

Parameters

$index

Returns

mixedvoid

 Properties

 

$cookies

$cookies : array

Default

array()
 

$headers

$headers : \Zend\Http\Headers

Default

null
inherited_from \Zend\Http\Headers::headers
 

Array of header array information or Header instances

$headers : array

Default

array()
 

key names for $headers array

$headersKeys : array

Default

array()
 

$pluginClassLoader

$pluginClassLoader : \Zend\Loader\PluginClassLocator

Default

null
 

$rawCookies

$rawCookies : array

Default

 Constants