What's the correct way to use Guzzle 6 to create pool of asynchronous json requests to send to API endpoints?

后端 未结 2 756
慢半拍i
慢半拍i 2021-01-18 07:07

My objective is to use Guzzle 6 to create a pool of asynchronous requests that PUT json data. Then monitor each $promise success/failure.

For comparison to my POOL c

相关标签:
2条回答
  • 2021-01-18 07:22

    If you want full control, don't use the Request() object in your Pool. Instead, start the request yourself by having your pool's generator yielding a callable function which starts the request. That gives you total control of all options. Here is a correct code example:

    https://stackoverflow.com/a/40622269/5562035

    0 讨论(0)
  • 2021-01-18 07:43

    Hopefully, someone else will jump in and let me know if there is a more correct way to accomplish my objective, but after looking under the hood in Guzzle I realized new Request()'s 3rd parameter was looking for header information, and the 4th parameter was looking for a body. So the following code works using the Pool.:

    foreach ($syncRequests as $key => $headers) {
        yield new Request('PUT', "{$uri}{$headers['endpoint']}", ['Content-type' => 'application/json'], json_encode(['json' => ['nonce' => $headers['json']]]));
    }
    

    Also in docs for Psr7\Request

    0 讨论(0)
提交回复
热议问题