|

What Is Guzzle PHP?

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!