@@ -62,8 +62,9 @@ requested URL::
62
62
$crawler = $client->request('GET', 'http://symfony.com');
63
63
64
64
The value returned by the ``request() `` method is an instance of the
65
- :class: `Symfony\\ Component\\ DomCrawler\\ Crawler ` class, which allows accessing
66
- and traversing HTML elements programmatically.
65
+ :class: `Symfony\\ Component\\ DomCrawler\\ Crawler ` class, provided by the
66
+ :doc: `DomCrawler component </components/dom_crawler >`_, and which allows
67
+ accessing and traversing HTML elements programmatically.
67
68
68
69
Clicking Links
69
70
~~~~~~~~~~~~~~
@@ -83,10 +84,12 @@ the needed HTTP GET request to simulate the link click::
83
84
Submitting Forms
84
85
~~~~~~~~~~~~~~~~
85
86
86
- The ``Crawler `` object is also capable of simulating form submissions. First,
87
- select the form via any of its buttons (thanks to the ``selectButton() `` and
88
- ``form() `` methods). Then, fill in the form data to send and use the ``submit() ``
89
- method to make the needed HTTP POST request to submit the form::
87
+ The ``Crawler `` object is also capable of selecting forms. First, select any of
88
+ the form's buttons with the ``selectButton() `` method. Then, use the ``form() ``
89
+ method to select the form which the button belongs to.
90
+
91
+ After selecting the form, fill in its data and send it using the ``submit() ``
92
+ method (which makes the needed HTTP POST request to submit the form contents)::
90
93
91
94
use Acme\Client;
92
95
@@ -125,17 +128,18 @@ retrieve any cookie while making requests with the client::
125
128
$cookie = $cookieJar->get('name_of_the_cookie');
126
129
127
130
// Get cookie data
128
- $name = $cookie->getName();
129
- $value = $cookie->getValue();
130
- $raw = $cookie->getRawValue();
131
- $secure = $cookie->isSecure();
131
+ $name = $cookie->getName();
132
+ $value = $cookie->getValue();
133
+ $raw = $cookie->getRawValue();
134
+ $secure = $cookie->isSecure();
132
135
$isHttpOnly = $cookie->isHttpOnly();
133
- $isExpired = $cookie->isExpired();
134
- $expires = $cookie->getExpiresTime();
135
- $path = $cookie->getPath();
136
- $domain = $cookie->getDomain();
136
+ $isExpired = $cookie->isExpired();
137
+ $expires = $cookie->getExpiresTime();
138
+ $path = $cookie->getPath();
139
+ $domain = $cookie->getDomain();
137
140
138
141
.. note ::
142
+
139
143
These methods only return cookies that have not expired.
140
144
141
145
Looping Through Cookies
@@ -154,22 +158,19 @@ Looping Through Cookies
154
158
155
159
// Get array with all cookies
156
160
$cookies = $cookieJar->all();
157
- foreach($cookies as $cookie)
158
- {
161
+ foreach ($cookies as $cookie) {
159
162
// ...
160
163
}
161
164
162
165
// Get all values
163
166
$values = $cookieJar->allValues('http://symfony.com');
164
- foreach($values as $value)
165
- {
167
+ foreach ($values as $value) {
166
168
// ...
167
169
}
168
170
169
171
// Get all raw values
170
172
$rawValues = $cookieJar->allRawValues('http://symfony.com');
171
- foreach($rawValues as $rawValue)
172
- {
173
+ foreach ($rawValues as $rawValue) {
173
174
// ...
174
175
}
175
176
@@ -182,13 +183,7 @@ into the client constructor::
182
183
use Acme\Client;
183
184
184
185
// create cookies and add to cookie jar
185
- $expires = new \DateTime();
186
- $expires->add(new \DateInterval('P1D'));
187
- $cookie = new Cookie(
188
- 'flavor',
189
- 'chocolate chip',
190
- $now->getTimestamp()
191
- );
186
+ $cookieJar = new Cookie('flavor', 'chocolate', strtotime('+1 day'));
192
187
193
188
// create a client and set the cookies
194
189
$client = new Client(array(), array(), $cookieJar);
@@ -204,17 +199,17 @@ history::
204
199
205
200
// make a real request to an external site
206
201
$client = new Client();
207
- $home_crawler = $ client->request('GET', 'http://symfony.com');
202
+ $client->request('GET', 'http://symfony.com');
208
203
209
204
// select and click on a link
210
- $doc_link = $crawler->selectLink('Documentation')->link();
211
- $doc_crawler = $ client->click($link);
205
+ $link = $crawler->selectLink('Documentation')->link();
206
+ $client->click($link);
212
207
213
208
// go back to home page
214
- $home_crawler = $client->back();
209
+ $crawler = $client->back();
215
210
216
211
// go forward to documentation page
217
- $doc_crawler = $client->forward();
212
+ $crawler = $client->forward();
218
213
219
214
You can delete the client's history with the ``restart() `` method. This will
220
215
also delete all the cookies::
@@ -223,10 +218,10 @@ also delete all the cookies::
223
218
224
219
// make a real request to an external site
225
220
$client = new Client();
226
- $home_crawler = $ client->request('GET', 'http://symfony.com');
221
+ $client->request('GET', 'http://symfony.com');
227
222
228
223
// delete history
229
224
$client->restart();
230
225
231
- .. _ Packagist : https://packagist.org/packages/symfony/browser-kit
232
- .. _ Goutte : https://github.com/fabpot/Goutte
226
+ .. _ `Packagist` : https://packagist.org/packages/symfony/browser-kit
227
+ .. _ `Goutte` : https://github.com/fabpot/Goutte
0 commit comments