Guzzle |异步请求|资源类型错误无效

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Guzzle |异步请求|资源类型错误无效相关的知识,希望对你有一定的参考价值。

我正在尝试链接http请求,其中第二个请求取决于第一个请求的响应。我遇到了Guzzle Client-> sendAsync()。

我得到的错误:

exception: "InvalidArgumentException"
file: "...guzzlehttppsr7srcfunctions.php"
line: 116
message: "Invalid resource type: array"

这是我到目前为止所拥有的:

$client = new Client([...]);
$headers = [...];
$req = new Psr7Request('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 Psr7Request('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();

关于我做错了什么的任何想法?

答案

您必须手动将PHP数组编码为JSON以与Psr7Request一起使用

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

或者使用->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();
另一答案

如果您想使用“json”传递参数,那么您必须修改您的代码,如下所示:

$secondRequest = new Psr7Request('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();
     });

请参阅此处的文档(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']
]);

有关详细信息,请查看@Alexey Shokov答案。

以上是关于Guzzle |异步请求|资源类型错误无效的主要内容,如果未能解决你的问题,请参考以下文章

Guzzle 异步请求不是真正的异步?

AWS S3 上传存储桶 - 资源类型无效

对 API 的请求在 Postman 中有效,但在我尝试使用 laravel/guzzle 时无效

phpunit Guzzle 异步请求

如何忽略 Guzzle 5 中的无效 SSL 证书错误

利用Guzzle实现另一种PHP异步发送邮件(laravel5.4)