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 how to process 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.
{ "require": { "guzzlehttp/guzzle": "~5.0" } }
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.
C:wampwwwguzzle>composer install Loading composer repositories with package information Installing dependencies (including require-dev) - Installing react/promise (v2.1.0) Downloading: 100% - Installing guzzlehttp/streams (3.0.0) Downloading: 100% - Installing guzzlehttp/ringphp (1.0.0) Downloading: 100% - Installing guzzlehttp/guzzle (5.0.1) Downloading: 100% Writing lock file Generating autoload files C:wampwwwguzzle>
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
<?php
require 'vendor/autoload.php';
use GuzzleHttpClient;
$client = new Client();
$response = $client->get('http://httpbin.org/get');
echo '<pre>';
print_r($response);
?>
The output of the $response looks like this.
GuzzleHttpMessageResponse Object ( [reasonPhrase:GuzzleHttpMessageResponse:private] => OK [statusCode:GuzzleHttpMessageResponse:private] => 200 [effectiveUrl:GuzzleHttpMessageResponse:private] => http://httpbin.org/get [headers:GuzzleHttpMessageAbstractMessage:private] => Array ( [access-control-allow-credentials] => Array ( [0] => true ) [access-control-allow-origin] => Array ( [0] => * ) [content-type] => Array ( [0] => application/json ) [date] => Array ( [0] => Wed, 22 Oct 2014 13:33:22 GMT ) [server] => Array ( [0] => gunicorn/18.0 ) [content-length] => Array ( [0] => 275 ) [connection] => Array ( [0] => keep-alive ) ) [headerNames:GuzzleHttpMessageAbstractMessage:private] => Array ( [access-control-allow-credentials] => Access-Control-Allow-Credentials [access-control-allow-origin] => Access-Control-Allow-Origin [content-type] => Content-Type [date] => Date [server] => Server [content-length] => Content-Length [connection] => Connection ) [body:GuzzleHttpMessageAbstractMessage:private] => GuzzleHttpStreamStream Object ( [stream:GuzzleHttpStreamStream:private] => Resource id #55 [size:GuzzleHttpStreamStream:private] => [seekable:GuzzleHttpStreamStream:private] => 1 [readable:GuzzleHttpStreamStream:private] => 1 [writable:GuzzleHttpStreamStream:private] => 1 [uri:GuzzleHttpStreamStream:private] => php://temp [customMetadata:GuzzleHttpStreamStream:private] => Array ( ) ) [protocolVersion:GuzzleHttpMessageAbstractMessage:private] => 1.1 )
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.
<?php
require 'vendor/autoload.php';
use GuzzleHttpClient;
$client = new Client();
$response = $client->get('http://httpbin.org/get');
$statuscode = $response->getStatusCode();
$reasonphrase = $response->getReasonPhrase();
echo 'The get request to http://httpbin.org/get has a response with a statuscode of '.$statuscode.' and a reasonphrase of '.$reasonphrase;
?>
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.
<?php
$body = $response->getBody();
echo $body;
?>
{ "args":{ }, "headers":{ "Connection":"close", "Host":"httpbin.org", "User-Agent":"Guzzle/5.0.1 curl/7.36.0 PHP/5.5.12", "X-Request-Id":"6af65b26-cd0f-4822-b923-e14f66590ea0" }, "origin":"204.79.197.200", "url":"http://httpbin.org/get" }
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!