Chamindar2002
Chamindar2002

Reputation: 223

Curl Response handling with Guzzle 6.2 with Laravel 5.3

I am trying to obtain a JSON response from an end point using Guzzle 6.2 with Laravel 5.3.

I am using the following code to make a get request:

$client = new GuzzleHttp\Client([
  'base_uri' => 'https://192.xx.xxx.xx6/',
  'timeout'  => 2.0
]);

$response = $client->request('GET',
  '/fineract-provider/api/v1/clients/388?tenantIdentifier=default&pretty=true', [
    'verify' => false,
    'auth' => ['<username>', '<password>']
  ]
);

var_dump($response);

Which outputs the following response:

Response {#282
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:7 [
    "Server" => array:1 [
      0 => "Apache-Coyote/1.1"
    ]
    "Access-Control-Allow-Origin" => array:1 [
      0 => "*"
    ]
    "Access-Control-Allow-Methods" => array:1 [
      0 => "GET, POST, PUT, DELETE, OPTIONS"
    ]
    "Content-Type" => array:1 [
      0 => "application/json"
    ]
    "Transfer-Encoding" => array:1 [
      0 => "chunked"
    ]
    "Vary" => array:1 [
      0 => "Accept-Encoding"
    ]
    "Date" => array:1 [
      0 => "Sat, 04 Feb 2017 15:51:10 GMT"
    ]
  ]
  -headerNames: array:7 [
    "server" => "Server"
    "access-control-allow-origin" => "Access-Control-Allow-Origin"
    "access-control-allow-methods" => "Access-Control-Allow-Methods"
    "content-type" => "Content-Type"
    "transfer-encoding" => "Transfer-Encoding"
    "vary" => "Vary"
    "date" => "Date"
  ]
  -protocol: "1.1"
  -stream: Stream {#280
    -stream: stream resource @297
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  }
}

But this is not the response I expect. However, when I make the same request in my browser it gives the correct output as below:

enter image description here

What am I doing wrong here?

Upvotes: 0

Views: 859

Answers (1)

Jeffrey
Jeffrey

Reputation: 473

The Response object contains more information than just the response. You can get the actual output like this:

$output = (string)$response->getBody();

It might be necessary (in certain cases) to cast the result to a string, because the actual result is a stream.

Guzzle documentation: Responses

Upvotes: 1

Related Questions