Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting expected response from Guzzle

I'm trying to build an endpoint that forwards the data passed to it to an API using the Slim PHP Framework and I'm having trouble getting my response from a Guzzle request.

$app->map( '/api_call/:method', function( $method ) use( $app ){
    $client = new GuzzleHttp\Client([
        'base_url' => $app->config( 'api_base_url' ),
        'defaults' => [
            'query'   => [ 'access_token' => 'foo' ],
        ]
    ]);

    $request = $client->createRequest( $app->request->getMethod(), $method, [
        'query' => $app->request->params()
    ]);

    var_dump( $client->send( $request )->getBody() );

})->via( 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' )->conditions( [ 'route' => '.+?' ] );`

This then gives me...

object(GuzzleHttp\Stream\Stream)[59]
  private 'stream' => resource(72, stream)
  private 'size' => null
  private 'seekable' => boolean true
  private 'readable' => boolean true
  private 'writable' => boolean true
  private 'meta' => 
    array (size=6)
     'wrapper_type' => string 'PHP' (length=3)
      'stream_type' => string 'TEMP' (length=4)
      'mode' => string 'w+b' (length=3)
      'unread_bytes' => int 0
      'seekable' => boolean true
      'uri' => string 'php://temp' (length=10)

...instead of the response of 'cool' I was expecting.

If I just var_dump $client->sendRequest( $request ) I get a 200 OK, and the url is what I expect, http://localhost:8000/test?access_token=foo.

I have another request, but only using $client->post(...) and it works fine without giving me the stream thing back.

I've tried reading the stream using the example at the bottom (http://guzzle.readthedocs.org/en/latest/http-client/response.html) but it's telling me feof doesn't exist.

Anyone have any idea what I'm missing or doing wrong here?

like image 440
Johnathan Barrett Avatar asked Apr 02 '14 22:04

Johnathan Barrett


People also ask

What is a message in guzzle?

Guzzle is an HTTP client that sends HTTP requests to a server and receives HTTP responses. Both requests and responses are referred to as messages. Guzzle relies on the guzzlehttp/psr7 Composer package for its message implementation of PSR-7.

Why does guzzle keep returning a 4xx error?

Sorry, something went wrong. If Guzzle sends a malformed request the remote server should return a 4xx status code and in an optimal case an error message too. I would contact the maintainers of this service and ask for their help, notify them about this case, they might want to fix this issue (at least the status code) on their side.

Why do I get 500 errors when sending requests with guzzle?

It's not completely clear to me: do you receive 500 errors from the server you send requests to with Guzzle or Guzzle causes your script to return with an 500 error. In the first case it could be that the request is malformed (but even then the remote server is supposed to return 4xx errors).

What to do when guzzle sends a malformed request?

If Guzzle sends a malformed request the remote server should return a 4xx status code and in an optimal case an error message too. I would contact the maintainers of this service and ask for their help, notify them about this case, they might want to fix this issue (at least the status code) on their side.


1 Answers

Could be;

$response = $client->send($request)->getBody()->getContents();
$response = $client->send($request)->getBody()->read(1024*100000);

This also work as a shorthand;

$response = ''. $client->send($request)->getBody();
$response = (string) $client->send($request)->getBody();

// see __toString() method for last examples: http://php.net/manual/en/language.oop5.magic.php#object.tostring

like image 97
K-Gun Avatar answered Oct 12 '22 09:10

K-Gun