什么是使用狂饮6,创建异步JSON请求池发送到API端点的正确方法是什么? [英] What's the correct way to use Guzzle 6 to create pool of asynchronous json requests to send to API endpoints?

查看:369
本文介绍了什么是使用狂饮6,创建异步JSON请求池发送到API端点的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用狂饮6 创建该PUT JSON数据的异步请求的池。然后,监控每个$承诺成功/失败。

有关相比,我的POOL code例如,客户机 - $>请求()以下的单个请求的第三个参数转换为EN codeD JSON,然后添加内容类型:应用程序/ JSON * *

  $ =客户端新([
    base_uri'=> BASE_URL。 试验/异步/',//基础URI用于与相对的请求
    超时=> 0,// 0没有超时操作,看承诺
]);$响应= $客户 - >请求('PUT','酷',['JSON'=>'富'=>'酒吧']);

在接收端点API,我可以通过执行读取来自单个请求的JSON上述以下内容:

  $ JSON =的file_get_contents('PHP://输入');
$ JSON = json_de code($ JSON,真正的);

使用href=\"http://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests\" rel=\"nofollow\">并发请求例如,在文档的使用新请求()的异步请求,我希望相同的参数(方法,URL端点,JSON标志)可以使用,如在单个$客户端 - >请求()上面的例子。然而,产生新的请求()不处理如 $客户 - &GT第三JSON参数;请求()什么是正确的狂饮功能,从我的泳池code调用正确设置JSON和内容类型?还是有更好的方式来创建异步请求的一个大型游泳池和监视他们的结局?

POOL code例如:

$这个 - > asyncRequests =
    [
        '端点'=> '凉'
    ]
    [
        '端点'=> 真棒
    ]
    [
        '端点'=> '疯'
    ]
    [
        '端点'=> '奇怪的'
    ]
];$客户端=新的客户端([
    base_uri'=> BASE_URL,//基础URI用于与相对的请求
    超时=> 0 // 0没有超时操作,看承诺
]);$请求=功能($ asyncRequests){
    $ URI = BASE_URL。 试验/异步/';    的foreach($ asyncRequests为$关键=> $数据){
        产生新的请求(PUT,{$ URI} {$数据['端点']},['JSON'=>'富'=>'酒吧']);
    }
};$池=新池($客户端,$请求($这个 - > asyncRequests),[
    '并发'=> 10,
    '应验'=>功能($响应,$指数){
        $这个 - > handleSuccessPromises($响应,$指数);
    },
    '拒绝'=>功能($原因,$指数){
        $这个 - > handleFailurePromises($原因,$指数);
    },
]);$承诺= $ pool->的承诺(); //初始化转移,并创建一个承诺$许诺,>等待(); //强制请求完成的游泳池。


解决方案

希望别人会跳​​,让我知道是否有实现我的目标,一个比较正确的做法,但在狂饮引擎盖下看,我意识到新申请()的第三个参数一直在寻找的头信息,而第4个参数一直在寻找一具尸体。所以下面code工作使用泳池:

 的foreach($ syncRequests为$关键=> $头){
    产生新的请求(PUT,{$ URI} {$头['端点']},['内容类型'=>'应用程序/ JSON'],json_en code(['JSON' = GT; ['随机数'= GT; $头['JSON']]]));
}

>

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 code example, the following single request to $client->request() converts the 3rd parameter to encoded json and then adds the Content-type:application/json.**

$client = new Client([
    'base_uri' => BASE_URL . 'test/async/', // Base URI is used with relative requests
    'timeout'  => 0, // 0 no timeout for operations and watching Promises
]);

$response = $client->request('PUT', 'cool', ['json' => ['foo' => 'bar']]);

On the receiving API endpoint, I can read the json from the single request above by doing the following:

$json = file_get_contents('php://input');
$json = json_decode($json, true);

Using the concurrent requests example in the docs, for creating a Pool of asynchronous requests using new Request(), I hoped the same parameters (method, url endpoint, json flag) could be used, as in the single $client->request() example above. However, yield new Request() does not handle the 3rd json parameter like $client->request(). What is the correct Guzzle function to call from my Pool code to set json and content-type correctly? Or is there a better way to create a large pool of asynchronous requests and monitor their outcome?

POOL code example:

$this->asyncRequests = [
    [
        'endpoint' => 'cool'
    ],
    [
        'endpoint' => 'awesome'
    ],
    [
        'endpoint' => 'crazy'
    ],
    [
        'endpoint' => 'weird'
    ]
];

$client = new Client([
    'base_uri' => BASE_URL, // Base URI is used with relative requests
    'timeout'  => 0 // 0 no timeout for operations and watching Promises
]);

$requests = function ($asyncRequests) {
    $uri = BASE_URL . 'test/async/';

    foreach ($asyncRequests as $key => $data) {
        yield new Request('PUT', "{$uri}{$data['endpoint']}", ['json' => ['foo' => 'bar']]);
    }
};

$pool = new Pool($client, $requests($this->asyncRequests), [
    'concurrency' => 10,
    'fulfilled' => function ($response, $index) {
        $this->handleSuccessPromises($response, $index);
    },
    'rejected' => function ($reason, $index) {
        $this->handleFailurePromises($reason, $index);
    },
]);

$promise = $pool->promise(); // Initiate the transfers and create a promise

$promise->wait(); // Force the pool of requests to complete.

解决方案

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

这篇关于什么是使用狂饮6,创建异步JSON请求池发送到API端点的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆