Skip to content

Commit ea3fd71

Browse files
robert Parkerjaviereguiluz
robert Parker
authored andcommitted
Adding documentation for cookies
1 parent 6b6e23a commit ea3fd71

File tree

1 file changed

+90
-2
lines changed

1 file changed

+90
-2
lines changed

components/browser_kit/introduction.rst

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,96 @@ You can get the form object by using the crawler to select the button and runnin
102102
Cookies
103103
-------
104104

105+
Retreiving Cookies
106+
~~~~~~~~~~~~~~~~~~
107+
108+
The Crawler has a cookieJar which is a container for storing and recieving cookies.
109+
110+
.. code-block:: php
111+
112+
use ACME\Client;
113+
114+
// Make a request
115+
$client = new Client();
116+
$crawler = $client->request('GET', 'http://symfony.com');
117+
118+
// Get the cookie Jar
119+
$cookieJar = $crawler->getCookieJar();
120+
121+
// Get a cookie by name
122+
$flavor = $cookieJar->get('flavor');
123+
124+
// Get cookie data
125+
$name = $flavor->getName();
126+
$value = $flavor->getValue();
127+
$raw = $flavor->getRawValue();
128+
$secure = $flavor->isSecure();
129+
$isHttpOnly = $flavor->isHttpOnly();
130+
$isExpired = $flavor->isExpired();
131+
$expires = $flavor->getExpiresTime();
132+
$path = $flavor->getPath();
133+
$domain = $flavor->getDomain();
134+
135+
Looping Through Cookies
136+
~~~~~~~~~~~~~~~~~~~~~~~
137+
138+
.. code-block:: php
139+
140+
use ACME\Client;
141+
142+
// Make a request
143+
$client = new Client();
144+
$crawler = $client->request('GET', 'http://symfony.com');
145+
146+
// Get the cookie Jar
147+
$cookieJar = $crawler->getCookieJar();
148+
149+
// Get array with all cookies
150+
$cookies = $cookieJar->all();
151+
foreach($cookies as $cookie)
152+
{
153+
// ...
154+
}
155+
156+
// Get all values
157+
$values = $cookieJar->allValues('http://symfony.com');
158+
foreach($values as $value)
159+
{
160+
// ...
161+
}
162+
163+
// Get all raw values
164+
$rawValues = $cookieJar->allRawValues('http://symfony.com');
165+
foreach($rawValues as $rawValue)
166+
{
167+
// ...
168+
}
169+
170+
.. note::
171+
These cookie jar methods only return cookies that have not expired.
172+
173+
Setting Cookies
174+
~~~~~~~~~~~~~~~
175+
176+
You can define create cookies and add them to a cookie jar that can be injected it into the client constructor.
177+
178+
.. code-block:: php
179+
180+
use ACME\Client;
181+
182+
// create cookies and add to cookie jar
183+
$expires = new \DateTime();
184+
$expires->add(new \DateInterval('P1D'));
185+
$cookie = new Cookie(
186+
'flavor',
187+
'chocolate chip',
188+
$now->getTimestamp()
189+
);
190+
191+
// create a client and set the cookies
192+
$client = new Client(array(), array(), $cookieJar);
193+
// ...
194+
105195
History
106196
-------
107197

@@ -138,8 +228,6 @@ You can restart the clients history with the restart method. This will also clea
138228
// restart history
139229
$client->restart();
140230
141-
Insulated Request
142-
-----------------
143231
144232
.. _Packagist: https://packagist.org/packages/symfony/browser-kit
145233
.. _Goutte: https://github.com/fabpot/Goutte

0 commit comments

Comments
 (0)