Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Created March 2, 2017 18:13
Show Gist options
  • Save sloanlance/d070599150ce6633630bfe9730b0e32b to your computer and use it in GitHub Desktop.
Save sloanlance/d070599150ce6633630bfe9730b0e32b to your computer and use it in GitHub Desktop.
PHP: Guzzle as replacement for direct use of Curl or PECL HTTP modules.

Copied from: https://github.com/IMSGlobal/caliper-php/issues/126#issuecomment-226624750

I've recently learned that Guzzle is a popular HTTP client library for PHP: http://guzzlephp.org/

This package has a lot of nice features, including:

  • Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...
  • Can send both synchronous and asynchronous requests using the same interface.
  • Uses PSR-7 interfaces for requests, responses, and streams. This allows you to utilize other PSR-7 compatible libraries with Guzzle.
  • Abstracts away the underlying HTTP transport, allowing you to write environment and transport agnostic code; i.e., no hard dependency on cURL, PHP streams, sockets, or non-blocking event loops.
  • Middleware system allows you to augment and compose client behavior.

I've emphasized the most attractive features. caliper-php's HttpRequestor class already abstracts cURL vs. pecl_http. While I think we still should offer Requestor classes that let the developer specifically select one of those libraries over the other, we should also replace the current abstraction with Guzzle. It's gotten a lot more field testing and may be more reliable.

The code is available in GitHub: https://github.com/guzzle/guzzle

It can be included in caliper-php by adding it to the composer.json file: https://packagist.org/packages/guzzlehttp/guzzle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment