简体   繁体   中英

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:

在此处输入图片说明

What am I doing wrong here?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM