Skip to content

Commit dcc2c35

Browse files
committed
[BrowserKit] Document the integration with HttpClient
1 parent 4f57153 commit dcc2c35

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

components/browser_kit.rst

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ The BrowserKit Component
1010

1111
.. note::
1212

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>`_.
1617

1718
Installation
1819
------------
@@ -279,6 +280,38 @@ also delete all the cookies::
279280
// reset the client (history and cookies are cleared too)
280281
$client->restart();
281282

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+
282315
Learn more
283316
----------
284317

0 commit comments

Comments
 (0)