【问题标题】:Guzzle | Async requests | Invalid resource type error暴饮暴食 |异步请求 |无效的资源类型错误
【发布时间】: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();

关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: php laravel http guzzle


    【解决方案1】:

    您必须手动将 PHP 数组编码为 JSON 才能与 Psr7\Request 一起使用

    $secondRequest = new Psr7\Request('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();
    

    【讨论】:

      【解决方案2】:

      如果你想使用“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();
           });
      

      请参阅此处的文档 (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 的答案了解更多详情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-28
        • 2013-03-27
        • 2015-10-20
        • 1970-01-01
        • 2019-01-04
        • 2015-08-12
        • 2019-05-25
        • 1970-01-01
        相关资源
        最近更新 更多