How do you log all API calls using Guzzle 6

You can use any logger which implements PSR-3 interface with Guzzle 6 I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example. use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use GuzzleHttp\MessageFormatter; use Monolog\Logger; $stack = HandlerStack::create(); $stack->push( Middleware::log( new Logger(‘Logger’), new MessageFormatter(‘{req_body} – {res_body}’) ) ); $client = new \GuzzleHttp\Client( [ ‘base_uri’ => … Read more

How to perform multiple Guzzle requests at the same time?

From the docs: http://guzzle3.readthedocs.org/http-client/client.html#sending-requests-in-parallel For an easy to use solution that returns a hash of request objects mapping to a response or error, see http://guzzle3.readthedocs.org/batching/batching.html#batching Short example: <?php $client->send(array( $client->get(‘http://www.example.com/foo’), $client->get(‘http://www.example.com/baz’), $client->get(‘http://www.example.com/bar’) ));

Guzzle 6 progress of download

Though, its not mentioned within the documentation, you can use the “progress” request option. References to it can be found here. $options = [ ‘progress’ => function ($dl_total_size, $dl_size_so_far, $ul_total_size, $ul_size_so_far) { // do something. } ];

Upload file using Guzzle 6 to API endpoint

The way you are POSTing data is wrong, hence received data is malformed. Guzzle docs: The value of multipart is an array of associative arrays, each containing the following key value pairs: name: (string, required) the form field name contents:(StreamInterface/resource/string, required) The data to use in the form element. headers: (array) Optional associative array of … Read more

Handle Guzzle exception and get HTTP body

Guzzle 6.x Per the docs, the exception types you may need to catch are: GuzzleHttp\Exception\ClientException for 400-level errors GuzzleHttp\Exception\ServerException for 500-level errors GuzzleHttp\Exception\BadResponseException for both (it’s their superclass) Code to handle such errors thus now looks something like this: $client = new GuzzleHttp\Client; try { $client->get(‘http://google.com/nosuchpage’); } catch (GuzzleHttp\Exception\ClientException $e) { $response = $e->getResponse(); $responseBodyAsString … Read more

3rd party dependency conflict in developing WordPress Plugin

Welcome to WordPress hell. We have 2018 and WordPress still does not have any dependency management and still didn’t notice Composer existence. WordPress ecosystem simply relies on assumption, that functions/classes names of plugins/themes should be unique. Obviously distributing popular 3rd-part Composer libraries with your plugin/theme is asking for trouble – it is easy to get … Read more

Catching exceptions from Guzzle

Depending on your project, disabling exceptions for guzzle might be necessary. Sometimes coding rules disallow exceptions for flow control. You can disable exceptions for Guzzle 3 like this: $client = new \Guzzle\Http\Client($httpBase, array( ‘request.options’ => array( ‘exceptions’ => false, ) )); This does not disable curl exceptions for something like timeouts, but now you can … Read more

Guzzle: handle 400 bad request

As written in Guzzle official documentation: http://guzzle.readthedocs.org/en/latest/quickstart.html A GuzzleHttp\Exception\ClientException is thrown for 400 level errors if the exceptions request option is set to true For correct error handling I would use this code: use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; try { $response = $client->get(YOUR_URL, [ ‘connect_timeout’ => 10 ]); // Here the code for successful request } … Read more