Guzzle

(More than just an

HR violation!)

Services

  • Ongoing transition to SOA across industry.
  • How to manage inter-process communication?

How about @file_get_contents()?

 

:D

No :(

cURL

  • Language-neutral, bindings for many different languages.
  • Very powerful, lots of options...
  • ...can be difficult to use, and PHP's bindings leave something to be desired.

Unmet dependancey at step 2, anyone?

Guzzle!

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

(So, basically, exactly what we use cURL for in web development most of the time.)

 

Guzzle manages persistent connections,

abstracts out the HTTP transport layer,

integrates with pretty much any underlying method of sending HTTP requests over the wire.

 

No more fiddling with cURL options, yay!

$client = new GuzzleHttp\Client();
$response = $client->get('http://guzzlephp.org');
$res = $client->get('https://api.github.com/user',
 ['auth' =>  ['user', 'pass']]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
var_export($res->json());
// Outputs the JSON decoded data

// Send an asynchronous request.
$req = $client->createRequest('GET', 'http://httpbin.org',
 ['future' => true]);
$client->send($req)->then(function ($response) {
    echo 'I completed! ' . $response;
});

Guzzle has:

  • Lots of connection options with reasonable defaults that can be overridden if needed, access to underlying cURL options, etc.
  • Pretty neat error-handling for synchronous and asynchronous requests
  • A full event handling system based off Symfony 2's EventDispatcher
  • It's deliciously testable and testably delicious! (Complete with mock node.js webserver!)

Where do I sign up?

Requirements: PHP 5.4

 

:(

 

(but you might be okay?)

Best installed using composer:

composer require guzzlehttp/guzzle

 

Extensive documentation available online at:

 

http://guzzlephp.org/.

Guzzle

By Jon Cantwell

Guzzle

  • 896