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