Laravel 5.1:Guzzle 6 return 响应无效

Laravel 5.1: Guzzle 6 return response is not working

我将 Gazzle 6 与 Laravel 5.1 一起使用,并且我有一个奇怪的行为 return 从我正在使用的 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
);

如果我 return 像前一个一样将数据作为数组 first_reportsecond_report 是空的。但是如果我 return 只是为了举例

return $first_report;

return $second_report;

每个报告的数据 return 都正确编辑,但我不知道那里有什么问题,因为我试过: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 文档与我用来获取结果的实际方法不匹配。不知道是不是过时了还是我做错了,不过这个方法对我有用。