Laravel 5.1:Guzzle 6返回响应无效


Laravel 5.1: Guzzle 6 return response is not working

我将Gazzle 6与Laravel 5.1一起使用,从我使用的API返回数据时出现了一种奇怪的行为。这是我的代码:

$data = array(
    'id' => '112233'
);
$from = 'Carbon'Carbon::now()->subDays(1)->format('d/m/Y');
$to = 'Carbon'Carbon::now()->subMonths(1)->format('d/m/Y');
$first_report = $this->client->post('https://my.service.com/reporting/execute/', [
    'auth' => ['myemail@email.com', 'mypassword'],
    'form_params' => ['data' => json_encode($data)]
]);

$second_report = $this->client->get('mysecondservice.com/reports', [
    'query' => [
        'account_auth_token' => '000011122223333444455556667778889999',
        'start_date' => $to,
        'end_date' => $from
    ]
]);
return array(
    'first_report' => $first_report,
    'second_report' => $second_report
);

如果我像上一个一样以数组的形式返回数据,则first_reportsecond_report为空。但如果我只返回例如

return $first_report;

return $second_report;

每个报告都会正确返回数据,但我不知道问题出在哪里,因为我尝试过:json_encode,甚至return$response()->json但仍然不起作用。

你知道发生了什么事吗?

我认为您需要在必须获得响应的对象上运行send()函数,然后在该对象上运行getBody()以获得响应对象,然后在其上运行getContents()以获得字符串形式的响应内容。所以总的来说,它看起来像

$first_report = $this->client->post('https://my.service.com/reporting/execute/', [
'auth' => ['myemail@email.com', 'mypassword'],
'form_params' => ['data' => json_encode($data)]
])->send()->getBody()->getContents();

$second_report = $this->client->get('mysecondservice.com/reports', [
'query' => [
    'account_auth_token' => '000011122223333444455556667778889999',
    'start_date' => $to,
    'end_date' => $from
]
])->send()->getBody()->getContents();

我发现Guzzle文档与我用来获得结果的实际方法不匹配。不确定它是过时的还是我做错了,但这种方法对我有效。