1

Laravel 5.1 で Gazzle 6 を使用していますが、使用している 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...を試してみたが、まだ機能していないため、何が問題なのかわかりません。

何が起こっているかについて何か考えはありますか?

4

1 に答える 1

2

I think you need to run the send() function on the object you have to get the response, then run getBody() on that to get the response object, then run getContents() on that to get the contents of the response as a string. So in total it would look like

$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();

I find the Guzzle documentation does not match up with the actual methods I use to get the results. Not sure if it's out of date or if I'm doing it wrong, but this method works for me.

于 2015-12-03T22:32:53.493 に答える