Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.1:Guzzle 6返回响应不工作_Php_Laravel 5.1_Guzzle6 - Fatal编程技术网

Php Laravel 5.1:Guzzle 6返回响应不工作

Php Laravel 5.1:Guzzle 6返回响应不工作,php,laravel-5.1,guzzle6,Php,Laravel 5.1,Guzzle6,我将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 =

我将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;

每个报表都正确返回了数据,但我不知道问题出在哪里,因为我尝试过:json\u 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文档与我用于获得结果的实际方法不匹配。不确定它是否过时或我是否做得不对,但这种方法对我来说很有效。

什么是
echo gettype($first\u report)给你?我刚刚找到了正确的函数:->getBody()->getContents(),因为->send()->getBody(true);没用,很高兴你能用上!根据docs,getBody()返回一个body对象,而getBody(true)返回一个包含body内容的字符串。是的,谢谢!您应该使用我使用的方法更新您的答案,以便其他具有相同问题的用户可以看到正确的答案。
$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();