SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

From PHP/cURL to Guzzle

In previous tutorial, we explained the difference among cUrl, libcurl and PHP/cURL and showed you how to get started with PHP/cURL.

In this tutorial, we are going to you a PHP package called Guzzle. Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. It is meant to be a replacement of your existing PHP/cURL usage in your code base. If you want to know why you should make the switch and how you can do so. Read on.

Limitations of PHP/cURL

We will have to admit that it is a bit misleading to say these are the limitations of PHP/cURL.We are addressing the limitations of PHP/cURL by comparing it directly to the benefits of Guzzle. If you are building a simple script and all you need is a few lines of PHP/cURL code to make an HTTP call, and you don't care about tests and code quality. It is totally fine to use PHP/cURL. In fact, it is always fine to use PHP/cURL as long as you are comfortable. There are no real technical limitations of PHP/cURL. So with that disclaimer, let’s move on.

Limitations we have analysed when using PHP/cURL:

  • Since PHP/cURL is using libcurl and libcurl is using curl. It is a requirement to install curl, the command line tool.If you are using a shared hosting, you might not have the control to install software packages.
  • PHP/cURL provides a lof of options to work with curl as shown on its official document. On one hand, it is good as it provides a lot of flexibility to work with. On the other hand, it is too verbose to work with. It does not provide the most pleasant API.
  • Unit testing is a pain if you are using PHP/cURL. There is no out of box support for unit testing.

Introduction to Guzzle

Below is the official explanation of Guzzle from its official site:

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.

Let's see how Guzzle overcomes those limitations we have addressed previously.

First Guzzle is an abstraction layer for HTTP request. It has no hard dependency on PHP/cURL, PHP streams, sockets, or non-blocking event loops. It encapsulations away the underlying HTTP transport. You can use PHP/cURL if you are required to, but it is not a must. So in the case of shared hosting environment, you can use another alternative if PHP/cURL is not feasible.

Secondly, Guzzle provides a very clean API to work with. Documentation is very important when working with a library.Guzzle has done a very good job by providing a comprehensive documentation. Technically, Guzzle is very well written by object-oriented programming and it uses PSR-7 as the HTTP interfaces. Which means it can work with any other libraries as long as they are compatible with PSR-7 interfaces.

Lastly, Guzzle has built-in unit testing support. It provides a comparably easy way to mock HTTP layer. Check out its documentation for more details.

Working with Guzzle

Time to get our hands dirty. In this section, we will complete the same task from the previous article. This time we will use Guzzle instead of PHP/cURL.

Install Guzzle using Composer. The most recent version of Guzzle at the time of writing is 6.

composer install guzzlehttp/guzzle:~6.0

Create an index.php and include Composer autoload file. Meanwhile, we will import the Guzzle class:

<?php
 
require 'vendor/autoload.php';
 
use GuzzleHttp\Client;

Instantiate a Guzzle client object with Google's root URL:

$client = new Client([
    'base_uri' => 'http://www.google.com',
]);

Make a GET request to Google's search URI along.Meanwhile, we pass the query parameter q:

?

$response = $client->request('GET', 'search', [
    'query' => ['q' => 'curl']
]);

Finally, we will output the response body:

echo $response->getBody();

The full source code is listed as shown blow:

<?php
 
require 'vendor/autoload.php';
 
use GuzzleHttp\Client;
 
$client = new Client([
    'base_uri' => 'http://www.google.com',
]);
 
$response = $client->request('GET', 'search', [
    'query' => ['q' => 'curl']
]);
 
echo $response->getBody();

As you might have seen, the code above is almost self-explanatory, thanks to solid OOP design of Guzzle.

The end

Hopefully, this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.