简体   繁体   English

枪口| 异步请求| 无效的资源类型错误

[英]Guzzle | Async requests | Invalid resource type error

I am trying to chain http requests, where the second request is dependent on the response from the first. 我正在尝试链接http请求,其中第二个请求取决于第一个请求的响应。 I came across Guzzle Client->sendAsync(). 我遇到了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 ? 对我在做什么错有任何想法吗?

You have to encode your PHP array to JSON manually to use with Psr7\\Request 您必须手动将PHP数组编码为JSON才能与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 或使用->postAsync()代替->sendAsync() ,这更容易

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

If you would like to pass the parameters using "json", then you have to modify your code like below : 如果您想使用“ json”传递参数,则必须像下面这样修改代码:

$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 ): 请参阅此处的文档( 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. 查看@Alexey Shokov答案以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM