【发布时间】:2019-04-20 21:33:32
【问题描述】:
我正在尝试链接 http 请求,其中第二个请求取决于第一个请求的响应。我遇到了 Guzzle Client->sendAsync()。
我得到的错误:
exception: "InvalidArgumentException"
file: "...\guzzlehttp\psr7\src\functions.php"
line: 116
message: "Invalid resource type: array"
这是我目前所拥有的:
$client = new Client([...]);
$headers = [...];
$req = new Psr7\Request('GET', '/api/someapi', $headers);
$finalResponse = $client->sendAsync($req)->then(function($response1) use ($client) {
$firstResponse = json_decode($response1->getBody()->getContents());
// $firstResponse is an array
$secondHeaders = [...];
$secondRequest = new Psr7\Request('POST', 'api/anotherapi', $searchHeaders, [
'json' => [
'field1' => 'val1',
'field2' => 'val2',
'field3' => json_encode($firstResponse),
'field4' => 'val3'
]
]);
$secondResponse = $client->sendAsync($searchRequest)->function($response2) use ($client) {
return $response2->getBody()->getContents();
});
return $secondResponse->wait();
});
return $finalResponse->wait();
关于我做错了什么有什么想法吗?
【问题讨论】: