Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Components
  4. Browser Kit
  5. The BrowserKit Component
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Installation
  • Basic Usage
    • Creating a Client
    • Making Requests
    • Clicking Links
    • Submitting Forms
  • Cookies
    • Retrieving Cookies
    • Looping Through Cookies
    • Setting Cookies
  • History

The BrowserKit Component

Edit this page

Warning: You are browsing the documentation for Symfony 2.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

The BrowserKit Component

The BrowserKit component simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically.

Installation

You can install the component in two different ways:

  • Install it via Composer (symfony/browser-kit on Packagist);
  • Use the official Git repository (https://github.com/symfony/browser-kit).

Basic Usage

Creating a Client

The component only provides an abstract client and does not provide any backend ready to use for the HTTP layer.

To create your own client, you must extend the abstract Client class and implement the doRequest() method. This method accepts a request and should return a response:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace Acme;

use Symfony\Component\BrowserKit\Client as BaseClient;
use Symfony\Component\BrowserKit\Response;

class Client extends BaseClient
{
    protected function doRequest($request)
    {
        // ... convert request into a response

        return new Response($content, $status, $headers);
    }
}

For a simple implementation of a browser based on the HTTP layer, have a look at Goutte. For an implementation based on HttpKernelInterface, have a look at the Client provided by the HttpKernel component.

Making Requests

Use the request() method to make HTTP requests. The first two arguments are the HTTP method and the requested URL:

1
2
3
4
use Acme\Client;

$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');

The value returned by the request() method is an instance of the Crawler class, provided by the DomCrawler component, which allows accessing and traversing HTML elements programmatically.

Clicking Links

The Crawler object is capable of simulating link clicks. First, pass the text content of the link to the selectLink() method, which returns a Link object. Then, pass this object to the click() method, which performs the needed HTTP GET request to simulate the link click:

1
2
3
4
5
6
use Acme\Client;

$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');
$link = $crawler->selectLink('Go elsewhere...')->link();
$client->click($link);

Submitting Forms

The Crawler object is also capable of selecting forms. First, select any of the form's buttons with the selectButton() method. Then, use the form() method to select the form which the button belongs to.

After selecting the form, fill in its data and send it using the submit() method (which makes the needed HTTP POST request to submit the form contents):

1
2
3
4
5
6
7
8
9
10
11
12
13
use Acme\Client;

// make a real request to an external site
$client = new Client();
$crawler = $client->request('GET', 'https://github.com/login');

// select the form and fill in some values
$form = $crawler->selectButton('Log in')->form();
$form['login'] = 'symfonyfan';
$form['password'] = 'anypass';

// submit that form
$crawler = $client->submit($form);

Cookies

Retrieving Cookies

The Crawler object exposes cookies (if any) through a CookieJar, which allows you to store and retrieve any cookie while making requests with the client:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use Acme\Client;

// Make a request
$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');

// Get the cookie Jar
$cookieJar = $crawler->getCookieJar();

// Get a cookie by name
$cookie = $cookieJar->get('name_of_the_cookie');

// Get cookie data
$name       = $cookie->getName();
$value      = $cookie->getValue();
$raw        = $cookie->getRawValue();
$secure     = $cookie->isSecure();
$isHttpOnly = $cookie->isHttpOnly();
$isExpired  = $cookie->isExpired();
$expires    = $cookie->getExpiresTime();
$path       = $cookie->getPath();
$domain     = $cookie->getDomain();

Note

These methods only return cookies that have not expired.

Looping Through Cookies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use Acme\Client;

// Make a request
$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');

// Get the cookie Jar
$cookieJar = $crawler->getCookieJar();

// Get array with all cookies
$cookies = $cookieJar->all();
foreach ($cookies as $cookie) {
    // ...
}

// Get all values
$values = $cookieJar->allValues('http://symfony.com');
foreach ($values as $value) {
    // ...
}

// Get all raw values
$rawValues = $cookieJar->allRawValues('http://symfony.com');
foreach ($rawValues as $rawValue) {
    // ...
}

Setting Cookies

You can also create cookies and add them to a cookie jar that can be injected into the client constructor:

1
2
3
4
5
6
7
8
use Acme\Client;

// create cookies and add to cookie jar
$cookieJar = new Cookie('flavor', 'chocolate', strtotime('+1 day'));

// create a client and set the cookies
$client = new Client(array(), array(), $cookieJar);
// ...

History

The client stores all your requests allowing you to go back and forward in your history:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Acme\Client;

// make a real request to an external site
$client = new Client();
$client->request('GET', 'http://symfony.com');

// select and click on a link
$link = $crawler->selectLink('Documentation')->link();
$client->click($link);

// go back to home page
$crawler = $client->back();

// go forward to documentation page
$crawler = $client->forward();

You can delete the client's history with the restart() method. This will also delete all the cookies:

1
2
3
4
5
6
7
8
use Acme\Client;

// make a real request to an external site
$client = new Client();
$client->request('GET', 'http://symfony.com');

// delete history
$client->restart();
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Show your Sylius expertise

Show your Sylius expertise

Measure & Improve Symfony Code Performance

Measure & Improve Symfony Code Performance

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Wojciech Zimoń, a Symfony contributor

Thanks Wojciech Zimoń for being a Symfony contributor

1 commit • 34 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia