8000 minor #11817 [BrowserKit] Document the integration with HttpClient (j… · symfony/symfony-docs@76be978 · GitHub
[go: up one dir, main page]

Skip to content

Commit 76be978

Browse files
committed
minor #11817 [BrowserKit] Document the integration with HttpClient (javiereguiluz)
This PR was squashed before being merged into the 4.3 branch (closes #11817). Discussion ---------- [BrowserKit] Document the integration with HttpClient Commits ------- 66a474f [BrowserKit] Document the integration with HttpClient
2 parents 745c407 + 66a474f commit 76be978

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

components/browser_kit.rst

Lines changed: 39 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,41 @@ 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\\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+
282318
Learn more
283319
----------
284320

0 commit comments

Comments
 (0)
0