Class Cookies

Summary

Fully Qualified Name: Zend\Http\Cookies
Extends: Headers

Description

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

Name Description Defined By
addCookie() Add a cookie to the class. Cookie should be passed either as a Zend\Http\Header\SetCookie object or as a string - in which case an object is created from the string. Cookies
addCookiesFromResponse() Parse an HTTP response, adding all the cookies set in that response Cookies
addHeader() Add a Header to this container, for raw values @see addHeaderLine() and addHeaders() Headers
addHeaderLine() Add a raw header line, either in name => value, or as a single string 'name: value' Headers
addHeaders() Add many headers at once Headers
clearHeaders() Clear all headers Headers
count() 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 Headers
current() Return the current value for this iterator, lazy loading it if need be Headers
forceLoading() By calling this, it will force parsing and loading of all headers, after this count() will be accurate Headers
fromResponse() 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. Cookies
fromString() Cookies
get() Get all headers of a certain name/type Headers
getAllCookies() Get all cookies in the cookie jar as an array Cookies
getCookie() Get a specific cookie according to a URI and name Cookies
getMatchingCookies() 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. Cookies
getPluginClassLoader() Return an instance of a PluginClassLocator, lazyload and inject map if necessary Headers
has() Test for existence of a type of header Headers
isEmpty() Tells if the array of cookies is empty Cookies
key() Return the current key for this object as an iterator Headers
next() Advance the pointer for this object as an iterator Headers
removeHeader() Remove a Header from the container Headers
reset() Empties the cookieJar of any cookie Cookies
rewind() Reset the internal pointer for this object as an iterator Headers
setPluginClassLoader() Set an alternate implementation for the PluginClassLoader Headers
toArray() Return the headers container as an array Headers
toString() Render all headers at once Headers
valid() Is this iterator still valid? Headers

Method Details

addCookie()

Add a cookie to the class. Cookie should be passed either as a Zend\Http\Header\SetCookie object or as a string - in which case an object is created from the string.

Parameter Name Type Description
$cookie \SetCookie|string
$refUri \Uri\Uri|string Optional

Returns:

addCookiesFromResponse()

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

Parameter Name Type Description
$response \Response
$refUri \Uri\Uri|string Requested

Returns:

addHeader()

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

Parameter Name Type Description
$header \Header\HeaderInterface

Returns: \Headers

addHeaderLine()

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

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

Parameter Name Type Description
$headerFieldNameOrLine string
$fieldValue string optional

Returns: \Headers

addHeaders()

Add many headers at once

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

Parameter Name Type Description
$headers array|\Traversable

Returns: \Headers

clearHeaders()

Clear all headers

Removes all headers from queue

Returns: \Headers

count()

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

Returns: int count of currently known headers

current()

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

Returns: array|\Header\HeaderInterface

forceLoading()

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

Returns: bool

fromResponse()

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.

Parameter Name Type Description
$response \Response HTTP
$refUri \Uri\Uri|string The

Returns: \Cookies

fromString()

Parameter Name Type Description
$string
$string

Returns: void

get()

Get all headers of a certain name/type

Parameter Name Type Description
$name string

Returns: bool|\Header\HeaderInterface|\ArrayIterator

getAllCookies()

Get all cookies in the cookie jar as an array

Parameter Name Type Description
$retAs int Whether

Returns: array|string

getCookie()

Get a specific cookie according to a URI and name

Parameter Name Type Description
$uri \Uri\Uri|string The
$cookieName string The
$retAs int Whether

Returns: \SetCookie|string

getMatchingCookies()

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.

Parameter Name Type Description
$uri string|\Uri\Uri URI
$matchSessionCookies bool Whether
$retAs int Whether
$now int Override

Returns: array|string

getPluginClassLoader()

Return an instance of a PluginClassLocator, lazyload and inject map if necessary

Returns: \PluginClassLocator

has()

Test for existence of a type of header

Parameter Name Type Description
$name string

Returns: bool

isEmpty()

Tells if the array of cookies is empty

Returns: bool

key()

Return the current key for this object as an iterator

Returns: mixed

next()

Advance the pointer for this object as an iterator

Returns: void

removeHeader()

Remove a Header from the container

Parameter Name Type Description
$header \Header\HeaderInterface

Returns: bool

reset()

Empties the cookieJar of any cookie

Returns: \Cookies

rewind()

Reset the internal pointer for this object as an iterator

Returns: void

setPluginClassLoader()

Set an alternate implementation for the PluginClassLoader

Parameter Name Type Description
$pluginClassLoader \Zend\Loader\PluginClassLocator

Returns: \Headers

toArray()

Return the headers container as an array

Returns: array

toString()

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.

Returns: string

valid()

Is this iterator still valid?

Returns: bool

Top