A 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\Client\Exception\InvalidArgumentException if invalid $cookie value

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

Required by Countable interface

count() : int

Returns

int

fromArray()

fromArray(array $values) 

Parameters

$values

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\Client\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

fromString()

fromString($string) 

Parameters

$string

get()

get($name, $default) 

Parameters

$name

$default

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\Cookie 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\Client\Cookie | 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\Cookie or as strings

Exceptions

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

Returns

\Zend\Http\Client\Cookiestring

Required by IteratorAggregate interface

getIterator() : \ArrayIterator

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\Client\Exception\InvalidArgumentException if invalid URI

Returns

arraystring

Tells if the array of cookies is empty

isEmpty() : bool

Returns

bool

(PHP 5 &gt;= 5.1.0)<br/> Whether a offset exists

offsetExists(mixed $offset) : bool

link http://php.net/manual/en/arrayaccess.offsetexists.php

Parameters

$offset

mixed

An offset to check for.

Returns

boolReturns true on success or false on failure.

The return value will be casted to boolean if non-boolean was returned.

(PHP 5 &gt;= 5.1.0)<br/> Offset to retrieve

offsetGet(mixed $offset) : mixed

link http://php.net/manual/en/arrayaccess.offsetget.php

Parameters

$offset

mixed

The offset to retrieve.

Returns

mixedCan return all value types.

(PHP 5 &gt;= 5.1.0)<br/> Offset to set

offsetSet(mixed $offset, mixed $value) : void

link http://php.net/manual/en/arrayaccess.offsetset.php

Parameters

$offset

mixed

The offset to assign the value to.

$value

mixed

The value to set.

(PHP 5 &gt;= 5.1.0)<br/> Offset to unset

offsetUnset(mixed $offset) : void

link http://php.net/manual/en/arrayaccess.offsetunset.php

Parameters

$offset

mixed

The offset to unset.

Empties the cookieJar of any cookie

reset() : \Zend\Http\Client\Cookies

(PHP 5 &gt;= 5.1.0)<br/> String representation of object

serialize() : string

link http://php.net/manual/en/serializable.serialize.php

Returns

stringthe string representation of the object or &null;

set()

set($name, $value) 

Parameters

$name

$value

toArray()

toArray() 

toString()

toString() 

(PHP 5 &gt;= 5.1.0)<br/> Constructs the object

unserialize(string $serialized) : mixed

link http://php.net/manual/en/serializable.unserialize.php

Parameters

$serialized

string

The string representation of the object.

Returns

mixedthe original value unserialized.

Helper function to recursively flatten an array.

_flattenCookiesArray(\Zend\Http\Header\Cookie | 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

 Properties

 

Array storing cookies

$cookies : array

Cookies are stored according to domain and path: $cookies + www.mydomain.com + / - cookie1 - cookie2 + /somepath - othercookie + www.otherdomain.net + / - alsocookie

 

The Zend\Http\Header\Cookie array

$rawCookies : array

 Constants