What Is Goutte?

Thanks For Sharing! :-)Share on Google+Tweet about this on TwitterShare on FacebookShare on RedditBuffer this page

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)
Handle User TimeZone on Symfony

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(Symfony\Component\DomCrawler\Crawler). There are three classes in the Symfony\Component\DomCrawler 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 :-)

Thanks For Sharing! :-)Share on Google+Tweet about this on TwitterShare on FacebookShare on RedditBuffer this page

What Is Guzzle PHP?

Thanks For Sharing! :-)Share on Google+Tweet about this on TwitterShare on FacebookShare on RedditBuffer this page

What Is Guzzle PHP

You may have heard the term Guzzle come up lately in the PHP Community and began to wonder what it is. We were curious as well so we decided to dig into it a bit more. In this post we’ll take a look at what Guzzle is, what it is used for, some of the projects that make use of it, and how you might be able to make use of it as well. There will be a little bit of network speak and discussion on web protocols, specifically HTTP, and how Guzzle works with it. Let’s jump into learning about Guzzle now.


What Is Guzzle?

Guzzle is an HTTP client built with and for PHP. The cURL software has typically handled all of the HTTP heavy lifting in PHP, or in some cases of quick hacking, the good old file_get_contents() function. Guzzle is a bit more advanced and simple at the same time. The software itself is quite impressive, providing a nice elegant solution to the developer that is easy to use. All the complexity is hidden away in the class implementation.


How Do You Install Guzzle?

To install Guzzle PHP, you’ll want to make use of the best thing to happen to PHP ever, The Great and Wonderful Composer. As we discussed many times here at VegiBit, Composer is really advancing the art of PHP while encouraging developers to share code like never before. So let’s go ahead and install Guzzle using Composer.

First up, let’s find the package on Packagist. To save you some typing, you can just click this Packagist Link and be rewarded.

Create A Composer File With The Guzzle Requirement

Next, we need to create a composer.json file in the directory of our choice. For this example, we’ll just go ahead and create a guzzle folder to hold our file. The composer.json will look like this.

Run Composer Install

Now that we have a folder to hold the contents and a composer.json file constructed, we can download the repository. Type composer install, and prosper.

Nice! If you made it this far, you now have a working copy of the Guzzle Software on you local machine. Now you can test it out a bit.

What Is Guzzle For?

Guzzle allows your application to make HTTP requests. This begs the question, “What can you make the request to?” Guzzle can make HTTP requests to any device that is capable of sending an HTTP response, whether that be an API from twitter, facebook, or reddit, or any public website. The official documentation uses http://httpbin.org/ for example calls, so let’s test those out.

Create a Client and Make a Request

The output of the $response looks like this.

Nice! We can see a ton of useful information about the response received right there.

Using A Guzzle Response

Just dumping the data out to the screen is not all that useful beyond learning and debugging. Guzzle provides some methods you can use to work with the response.

The get request to http://httpbin.org/get has a response with a statuscode of 200 and a reasonphrase of OK

Accessing The Guzzle Response Body

Now that we know how to work with the HTTP status codes in our responses, let’s look at how to parse the data in the response body. We can use the Guzzle getBody() method to do this.

What Else Can Guzzle Do?

Guzzle can do a lot, and this is why you see so many popular projects in the PHP community having Guzzle as a dependency. It really takes working with HTTP in PHP and puts it on Steroids. In addition to the simple requests we did here as a proof of concept type test, Guzzle can also manage json responses, fetch xml data, modify HTTP headers, upload data, send post requests, maintain cookie sessions, and handle exceptions. Learn all about Guzzle at the official docs.

Thank you for reading What Is Guzzle PHP?If you think Guzzle is pretty cool as well, please do share using the buttons below!

Thanks For Sharing! :-)Share on Google+Tweet about this on TwitterShare on FacebookShare on RedditBuffer this page