HTTP Client That Handles Retries and Logging

Packages

November 1st, 2019

http-client-wrapper.png

Gustavo Ocanto created an HTTP client on top of Guzzle that handles retries and logging. Typically you’d have some code like the following (and likely a try/catch too) if you need to retry an HTTP call:

1use GuzzleHttp\Client;
2
3$retry = 1;
4$response = null;
5
6do {
7 $response = (new Client)->get('http://foo.com');
8} while ($response === null && $retry <= 5);

With this client, you can achieve similar with the following code:

1$response = (new Client)->retry(5)->get('http://foo.com');

If you need more granular control and want to tap into the retry routine, the package has an onRetry method:

1$response = (new Client)->onRetry(function () {
2 // Do stuff
3})->get('http://foo.com');

You can learn more about this package, get full installation instructions, and view the source code on GitHub at gocanto/http-client.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.