Guzzle – PHP HTTP Client

Posted by Unknown on Sunday, January 19, 2014

As you probably know, website development can be broken up into 2 main areas:



  • Front end (what the end user sees)

  • Back end (what the server has to do in order to provide the requested data)


While front end development frequently uses several data sources to display a page, simple dynamic sites would only depend on data coming from a database hosted on the same server. As back end developers today, we are in need of retrieving data from a database server on a different host, or consuming an API from a third-party provider, while making it look like everything happened in our server.


PHP comes with the native cURL client (if enabled), that can be frightening to the newcomer or hard to use to consume SOAP services. Other frameworks implement their versions of HTTP, REST, and SOAP clients to some degree. However, if you do not wish to use cURL, you do not have a framework or you are not a fan of your framework's solution, Guzzle to the rescue.


Guzzle is an independent HTTP client for PHP. Installing Guzzle is very easy: you'll need to get composer first (http://www.sitepoint.com/php-dependency-management-with-composer/). Once you got composer installed and ready, create your basic composer.json file, which would look like the following:



{
"name": "jd/guzzle demo",
"authors": [
{
"name": "John Doe",
"email": "john@doe.tst"
}
],
"require": {

}
}


Now just add the guzzle library to the composer.json file in the require section as follows:



{
"name": "jd/guzzle demo",
"authors": [
{
"name": "John Doe",
"email": "john@doe.tst"
}
],
"require": {
"guzzle/guzzle": "~3.7"
}
}


Having saved the file, you just need to run the composer install command and you should be good to go!


Basics


As a cryptography enthusiast, searching for sources of entropy data for key generation is a hobby of mine. One good source is the ANU Quantum Random Numbers Server at https://qrng.anu.edu.au. Supposedly their RNG is based on quantum physics, so it is worth taking a look at when generating random data with a good entropy level.


Fortunately they have a public API we can consume, so here is where Guzzle will come in handy:



<?php
chdir(dirname(__DIR__));

require_once 'vendor/autoload.php';

use Guzzle\Http\Client;
use Guzzle\Http\EntityBody;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;

/** @var $client Client */
$client = new Client("https://qrng.anu.edu.au");


The first two lines should be familiar to anyone using composer and its autoloading mechanisms. When we instantiate a Client object, we should pass as a constructor parameter the URL we want to connect to. Please notice that it does not use path and/or URL parameters – this will come in a minute.



/** @var $request Request */
$request = $client->get('/API/jsonI.php?length=10&type=uint8');

/** @var $response Response */
$response = $request->send();

/** @var $body EntityBody */
$body = $response->getBody(true);


We finally indicate the path (/API/jsonI.php) and the URL parameters (length=10&type=uint8) to the client, building an HTTP GET request via the $client->get() method. If we wanted to build a POST request, the client object has post() and many other HTTP request methods. For sending the request, the $request->send() method should be invoked, catching the response in the $response variable. Finally if we want to see what the response from the remote server was, we execute the $response->getBody() method. TRUE can be sent as a parameter to the getBody() method to get the result as a string.


Path and URL Parameters Manipulation


In the previous example, we used a string to indicate the path and URL parameters to build the request… This might not be the most elegant solution. We might decide to pass on certain URL parameters or use different paths according to certain conditions. We can easily accomplish proper handling of path and URL parameters by


Truncated by Planet PHP, read more at the original (another 9984 bytes)




more

{ 0 comments... » Guzzle – PHP HTTP Client read them below or add one }

Post a Comment

Popular Posts