发送多个请求时如何将结果与请求匹配? [英] How to match a result to a request when sending multiple requests?

查看:124
本文介绍了发送多个请求时如何将结果与请求匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A.摘要

作为标题,Guzzle允许一次发送多个请求以节省时间,如文档.

As its title, Guzzle allows to send multiple requests at once to save time, as in documentation.

$responses = $client->send(array(
    $requestObj1,
    $requestObj2,
    ...
));

(given that each request object is an instance of
Guzzle\Http\Message\EntityEnclosingRequestInterface)

当响应返回时,要确定哪个响应是针对哪个请求的,我们可以遍历每个请求并获取响应(仅在执行上述命令后可用):

When responses come back, to identify which response is for which request, we can loop through each request and get the response (only available after executing the above command):

$response1 = $requestObj1->getResponse();
$response2 = $requestObj2->getResponse();
...

B.问题

如果请求对象包含相同的数据.无法确定原始请求.

If the request object contains the same data. It's impossible to identify the original request.

假设我们有以下情形,需要创建2篇文章:远程服务器上的A和B:something.com/articles/create.json

Assume we have the following scenario where we need to create 2 articles: A and B on a distance server: something.com/articles/create.json

每个请求具有相同的POST数据:

Each request has same POST data:

subject: This is a test article

创建后,带有2个位置的Guzzle响应会返回:

After created, the Guzzle responses with 2 location come back:

something.com/articles/223.json
something.com/articles/245.json

使用上述方法将响应链接到它的请求,我们仍然不知道哪个响应是针对哪个文章的,因为请求对象是完全相同的.

Using the above method to link response-to-its-request, we still don't know which response is for which article, because the request object is exactly the same.

因此在我的数据库中,我无法写下结果:

Hence in my database I cannot write down the result:

article A -> Location: 245.json
article B -> Location: 223.json

因为它可能是相反的方式:

because it can be the other way arround:

article A -> Location: 223.json
article B -> Location: 245.json

一种解决方案是在POST请求中添加一些额外的参数,例如

A solution is to put some extra parameter in the POST request, e.g.

subject: This is a test article
record: A

但是,距离服务器将返回错误并且不创建商品,因为它不理解键记录".距离服务器是第三方服务器,我无法更改其工作方式.

However, the distance server will return error and does not create article because it does not understand the key "record". The distance server is a third party server and I cannot change the way it works.

另一个合适的解决方案是在请求对象上设置一些特定的id/tag,这样我们以后就可以识别它.但是,我浏览了文档,但是没有方法可以像

Another proper solution for this is to set some specific id/tag on the request object, so we can identify it afterwards. However, I've looked through the documentation but there is no method to uniquely identity the request like

$request->setID("id1")

or

$request->setTag("id1")

这困扰了我好几个月,仍然无法解决此问题.

This has been bugging me for months and still cannot resolve this issue.

如果您有解决方案,请告诉我.

If you have solution, please let me know.

非常感谢,您已经救了我!!!

Many many thanks and you've saved me!!!!

感谢您阅读这篇长文章.

Thanks for reading this long post.

推荐答案

我找到了一种正确的方法,Guzzle允许在请求完成后添加回调.因此,我们可以通过在批处理中的每个请求上对其进行设置来实现此目的

I've found a proper way to do it, Guzzle allow to add callback once a request is completed. So we can achieve this by setting it on each request in the batch

默认情况下,每个请求都可以这样创建

Each request by default can be created like this

$request = $client->createRequest('GET', 'http://httpbin.org', [
    'headers' => ['X-Foo' => 'Bar']
]);

因此,要实现我们想要的目标:

So, to achieve what we want:

$allRequests = [];
$allResults = [];

for($k=0; $k<=10; $k++){
    $allRequests['key_'.$k] = $client->createRequest('GET', 'http://httpbin.org?id='.$k, [
        'headers' => ['X-Foo' => 'Bar'],
        'events' => [
            'complete' => function ($e) use (&$allResults, $k){
                $response = $e->getResponse();
                $allResults['key_'.$k] = $response->getBody().'';
            }
        ]
    ]);
}

$client->sendAll(array_values($allRequests));

print_r($allResults);

因此,$ allResults现在具有每个相应请求的结果.

So now the $allResults has result for each corresponding request.

例如$ allResults ['key_1']是$ allRequests ['key_1']

e.g. $allResults['key_1'] is the result of $allRequests['key_1']

这篇关于发送多个请求时如何将结果与请求匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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