Guzzle | Async requests | Invalid resource type error

二次信任 提交于 2019-12-25 01:34:35

问题


I am trying to chain http requests, where the second request is dependent on the response from the first. I came across Guzzle Client->sendAsync().

The error I get:

exception: "InvalidArgumentException"
file: "...\guzzlehttp\psr7\src\functions.php"
line: 116
message: "Invalid resource type: array"

Here's what I have so far:

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

Any thoughts about what I'm doing wrong ?


回答1:


You have to encode your PHP array to JSON manually to use with Psr7\Request

$secondRequest = new Psr7\Request('POST', 'api/anotherapi', $searchHeaders, json_encode([
    'field1' => 'val1',
    'field2' => 'val2',
    'field3' => json_encode($firstResponse),
    'field4' => 'val3'
]));

Or use ->postAsync() instead of ->sendAsync(), it's easier

$client = new Client();
$headers = [];
$finalResponse = $client->getAsync('/api/someapi', ['headers' => $headers])
    ->then(function ($response1) use ($client) {
        $firstResponse = json_decode($response1->getBody()->getContents());
        // $firstResponse is an array
        $secondHeaders = [];
        $secondResponse = $client->postAsync('api/anotherapi', [
            'headers' => $secondHeaders,
            'json' => [
                'field1' => 'val1',
                'field2' => 'val2',
                'field3' => json_encode($firstResponse),
                'field4' => 'val3'
            ],
        ])->then(function ($response2) use ($client) {
            return $response2->getBody()->getContents();
        });

        // You don't need to call ->wait() here, Guzzle will resolve the promise for you
        return $secondResponse;
    });

return $finalResponse->wait();



回答2:


If you would like to pass the parameters using "json", then you have to modify your code like below :

$secondRequest = new Psr7\Request('POST', 'api/anotherapi', $searchHeaders);
     $secondResponse = $client->sendAsync($searchRequest, [
         'json' => [
         'field1' => 'val1',
         'field2' => 'val2',
         'field3' => json_encode($firstResponse),
         'field4' => 'val3'
        ])->function($response2) use ($client) {
          return $response2->getBody()->getContents();
     });

Refer to the documentation here (http://docs.guzzlephp.org/en/stable/quickstart.html):

An easy way to upload JSON data and set the appropriate header is using the json request option:

$r = $client->request('PUT', 'http://httpbin.org/put', [
    'json' => ['foo' => 'bar']
]);

Check @Alexey Shokov answer for more details.



来源:https://stackoverflow.com/questions/55751046/guzzle-async-requests-invalid-resource-type-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!