What Is Goutte?

What Is Goutte

At the time of this writing, Laravel 5 development is in full swing – and there are lots of changes coming down the pike. It would be fun to jump back in to covering Laravel but things seem a little too fluid at the moment. We’ll know by the end of the year where to focus our energies when working with Laravel once the feature set and best practices are agreed upon and shipped. In the meantime, let’s take a look at some of the various PHP repositories that might be fun to tinker with. In this episode, we’ll take a look at Goutte which is written by the legendary fabpot, or Fabien Potencier. Fabien is the creator of the well known Symfony Framework which has components that are in use by many projects in the PHP community.

The Components of Goutte

Goutte is a wrapper for the popular Guzzle package, combined with three other symfony components. Those are Browser Kit, Css Selector, and the Dom Crawler. These are good sources of documentation for using Goutte, since there really isn’t any for the Goutte package itself. It will be a matter of simply installing, then kicking the tires to see what you can find.

Installing Goutte

To your own copy of the package to play around with, head on over to Packagist to find the requirements for your composer.json file. Oh heck, no need, here it is for you:

    "name": "fabpot/goutte",
    "type": "application",
    "description": "A simple PHP Web Scraper",
    "keywords": ["scraper"],
    "homepage": "https://github.com/fabpot/Goutte",
    "license": "MIT",
    "authors": [
            "name": "Fabien Potencier",
            "email": "fabien@symfony.com"
    "require": {
        "php": ">=5.4.0",
        "symfony/browser-kit": "~2.1",
        "symfony/css-selector": "~2.1",
        "symfony/dom-crawler": "~2.1",
        "guzzlehttp/guzzle": "4.*"
    "autoload": {
        "psr-0": { "Goutte": "." }
    "extra": {
        "branch-alias": {
            "dev-master": "2.0-dev"

Start Testing Your New Goutte Install

With your new software installed and ready to go, let’s go ahead and see what we can do with it.


// use composer to autoload Goutte
require 'vendor/autoload.php';

// get your namespaces right
use GoutteClient;

// create a new client, via Guzzle
$client = new Client();

// Check out the symfony.com subreddit and request the top posts from this month
$crawler = $client->request('GET', 'http://www.reddit.com/r/symfony/top/?sort=top&t=month');

// See if the response was ok
$status_code = $client->getResponse()->getStatus();
    echo '200 OK<br>';

// Use the symfony filter method to find all links which are children of paragraph
// elements which have a class of title then loop through the results using the each method

$crawler->filter('p.title > a')->each(function ($node) {
    echo '<a target="blank" rel="noopener">attr('href').'" >'.htmlentities($node->text()).'</a><br>';


200 OK
Introducing the Official Symfony Best Practices
New in Symfony 2.6: Bootstrap form theme
New in Symfony 2.6: AJAX requests in the web debug toolbar
New in Symfony 2.6: Farewell to ICU component (Symfony Blog)
New in Symfony 2.6: LockHandler
Symfony 2.5.5 released
New in Symfony 2.6: Date support for Validator constraints
Commerce Guys makes big investment in Symfony, for eCommerce and Platform.sh
Symfony, 9 years of history, rewards its top 150 contributors
Let’s revive Symfony Montreal meetups!
New in Symfony 2.6: Smarter assets:install command
Symfony 2.6 fast approaching its stabilization phase
New in Symfony 2.6: New shortcut methods for controllers
A week of symfony #406 (06-&gt;12 October 2014)
Tell Doctrine to use a different database user when using the symfony console?
Symfony 2.4.10 released
A week of symfony #407 (13-&gt;19 October 2014)

Awesome! With the Goutte software we were able to fetch all the links from the Symfony subreddit.

Goutte vs SimpleHtmlDom

Simple HTML Dom is another handy piece of software for doing tricks like this. It is a bit easier to use than Goutte, however if you learn all the details of how Goutte works, it may help with your Symfony chops. In dealing with Laravel, which relies on several Symfony dependencies in order to work, many have become more interested in what Symfony has to offer.

Symfony Dom Crawler

Let’s look a little bit more at the script above. When we ran $client->request(), a var dump of the $crawler variable gives us object(SymfonyComponentDomCrawlerCrawler). There are three classes in the SymfonyComponentDomCrawler namespace. You have the Crawler class itself, which helps to navigate a list of Dom Elements. The Form and Link classes allow you to programmatically interact with forms and links on a webpage via PHP. It’s almost like creating a virtual web broswer via PHP, which is really kind of cool. To see all the options available, visit the API.

Log In To A Website With Goutte

Now you may be saying, yeah yeah yeah, we can do all of this in native PHP. To be fair, sure, you can. Get a load of this however. We can also log in to websites and navigate as a logged in user using Goutte. We got a little mind blowing stuff happening coming up, check this out.


// use composer to autoload Goutte
require 'vendor/autoload.php';

// get your namespaces right
use GoutteClient;

// create a new client, via Guzzle
$client = new Client();

$crawler = $client->request('GET', 'https://github.com/login');

// select the form and fill in some values
$form = $crawler->selectButton('Sign in')->form();
$form['login'] = 'your user name';
$form['password'] = 'your password';

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

// we are now logged in and can navigate the site
// Click the Explore Link
$link = $crawler->selectLink('Explore')->link();
$crawler = $client->click($link);

// Click the Front End Javascript Frameworks link
$link = $crawler->selectLink('Front-end JavaScript frameworks')->link();
$crawler = $client->click($link);

// get the a elements that are children of h3 elements
// the repository names are here
$crawler->filter('h3 > a')->each(function ($node) {
    echo '<b>'.$node->text().'</b><br>';


Epic! Try it out for yourself! You can try your credentials as well on your own setup and see how to navigate the site via PHP. We’re not sure what the use case for this would be, but nonetheless, it’s pretty impressive and a testament to the power of the software.


In this episode, we took a look at Goutte, the powerful combination of Guzzle and Symfony Components such as Browser Kit, Css Selector, and Dom Crawler. It’s fun to play around with these tools for learning purposes, and no doubt forces you to dig into the structure of the DOM, which is sometimes a little tricky.

Thanks for reading What Is Goutte?Please tweet or share, or something using the buttons below 🙂