@@ -10,9 +10,10 @@ The BrowserKit Component
10
10
11
11
.. note ::
12
12
13
- The BrowserKit component can only make internal requests to your application.
14
- If you need to make requests to external sites and applications, consider
15
- using `Goutte `_, a simple web scraper based on Symfony Components.
13
+ In Symfony versions prior to 4.3, the BrowserKit component could only make
14
+ internal requests to your application. Starting from Symfony 4.3, this
15
+ component can also :ref: `make HTTP requests to any public site <component-browserkit-external-requests >`
16
+ when using it in combination with the :doc: `HttpClient component </components/http_client >`.
16
17
17
18
Installation
18
19
------------
@@ -279,6 +280,41 @@ also delete all the cookies::
279
280
// reset the client (history and cookies are cleared too)
280
281
$client->restart();
281
282
283
+ .. _component-browserkit-external-requests :
284
+
285
+ Making External HTTP Requests
286
+ -----------------------------
287
+
288
+ So far, all the examples in this article have assumed that you are making
289
+ internal requests to your own application. However, you can run the exact same
290
+ examples when making HTTP requests to external web sites and applications.
291
+
292
+ First, install and configure the :doc: `HttpClient component </components/http_client >`.
293
+ Then, use the :class: `Symfony\\ Component\\ BrowserKit\\ HttpBrowser ` to create
294
+ the client that will make the external HTTP requests::
295
+
296
+ use Symfony\Component\BrowserKit\HttpBrowser;
297
+ use Symfony\Component\HttpClient\HttpClient;
298
+
299
+ $browser = new HttpBrowser(HttpClient::create());
300
+
301
+ You can now use any of the methods shown in this article to extract information,
302
+ click links, submit forms, etc. This means that you no longer need to use a
303
+ dedicated web crawler or scraper such as `Goutte `_::
304
+
305
+ $browser = new HttpBrowser(HttpClient::create());
306
+
307
+ $browser->request('GET', 'https://github.com');
308
+ $browser->clickLink('Sign in');
309
+ $browser->submitForm('Sign in', ['login' => '...', 'password' => '...']);
310
+ $openPullRequests = trim($browser->clickLink('Pull requests')->filter(
311
+ '.table-list-header-toggle a:nth-child(1)'
312
+ )->text());
313
+
314
+ .. versionadded :: 4.3
315
+
316
+ The feature to make external HTTP requests was introduced in Symfony 4.3.
317
+
282
318
Learn more
283
319
----------
284
320
0 commit comments