@@ -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,38 @@ 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\\HttpClient\\BrowserClient` to create
294
+ the client that will make the external HTTP requests::
295
+
296
+ use Symfony\Component\HttpClient\BrowserClient;
297
+ use Symfony\Component\HttpClient\HttpClient;
298
+
299
+ $browser = new BrowserClient(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
+ // ...
306
+ $browser = new BrowserClient(HttpClient::create());
307
+
308
+ $browser->request('GET', 'https://github.com');
309
+ $browser->clickLink('Sign in');
310
+ $browser->submitForm('Sign in', ['login' => '...', 'password' => '...']);
311
+ $openPullRequests = trim($browser->clickLink('Pull requests')->filter(
312
+ '.table-list-header-toggle a:nth-child(1)'
313
+ )->text());
314
+
282
315
Learn more
283
316
----------
284
317
0 commit comments