|

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:

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.

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->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->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.

angular/angular.js
jashkenas/backbone
emberjs/ember.js
knockout/knockout
tastejs/todomvc
spine/spine
Polymer/polymer
mozbrick/brick
facebook/react

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.

Conclusion

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 🙂