2
votes

I use GuzzleHttp with an API REST. My parameters are in the

$url_complete = 'http://apirest.com/?'.$params;
$request = $client->post(
    $url_complete,
);

When I search a solution I get only solutions (http://guzzle3.readthedocs.org/http-client/entity-bodies.html) for EntityBody object. But EntityBody is the response of API. I don't need to read compress data I must send it.

Do you know a way to send compress (with gzip) data to an API REST by using GuzzleHttp ?

1
Are you restricted to using Guzzle 3 or do you have the ability to upgrade to Guzzle 6?Shaun Bramley
Yes, I can't use PHP 5.5 due to system administor restrictions.Samuel Dauzon

1 Answers

0
votes

To quote Ref #1:

Entity body is the term used for the body of an HTTP message. The entity body of requests and responses is inherently a PHP stream in Guzzle.

This means that the EntityBody object can be used with HTTP requests and responses.

The method signature for Client::post() is post($uri = null, $headers = null, $postBody = null, array $options = array()). As seen within your code snippet, you are not setting the body of the post request. Infact all you are doing is setting some uri query parameters.

Based on Ref #2, #3, and #4: you will probably want to do something like:

$body = EntityBody::factory(fopen($file_location));
$body->compress(); //compresses the body using the deflate php stream filter

$request = $client->post($uri, $headers, $body, $options);

$response = $client->send($request);

I should state that I have never actually done this, I have been using v4+ for the bulk of the last 1.5 yr and I am unable to test this out at the moment.

References:

  1. Entity Bodies
  2. Entity Bodies - Compression
  3. Using Request Objects - POST Requests
  4. Guzzle 3 - Client.php