Php Guzzle:使用Guzzle';s池:批处理()和'sink'选项
您可以使用Guzzle的Php Guzzle:使用Guzzle';s池:批处理()和'sink'选项,php,guzzle,guzzle6,Php,Guzzle,Guzzle6,您可以使用Guzzle的Pool:batch()方法并行执行http请求。它允许您使用第三个参数中的options键设置请求的默认选项 但是如果池中的不同请求需要不同的选项怎么办?我希望使用池执行GET请求,并将每个响应流式传输到磁盘上的不同文件。有一个sink选项可供选择。但是如何将此选项的不同值应用于请求?您可以分别为请求指定所需的$options。只有将其传递给客户端时,它才会应用于所有请求。以下是Guzzle 6文档的摘录: 在创建客户端时,可以将标题添加为默认选项。什么时候 标题用作默
Pool:batch()
方法并行执行http请求。它允许您使用第三个参数中的options
键设置请求的默认选项
但是如果池中的不同请求需要不同的选项怎么办?我希望使用池执行GET请求,并将每个响应流式传输到磁盘上的不同文件。有一个
sink
选项可供选择。但是如何将此选项的不同值应用于请求?您可以分别为请求指定所需的$options
。只有将其传递给客户端时,它才会应用于所有请求。以下是Guzzle 6文档的摘录:
在创建客户端时,可以将标题添加为默认选项。什么时候
标题用作默认选项,仅当
正在创建的请求尚未包含特定标头。
这包括在send()和
sendAsync()方法和客户端创建的请求(例如。,
request()和requestAsync()
有关guzzle 6,请参见
$client = new \GuzzleHttp\Client();
$requests = function ($total) use ($client) {
for ($i = 0; $i < $total; $i++) {
$url = "http://domain.com/picture/{$i}.jpg";
$filepath = "/tmp/{$i}.jpg";
yield function() use ($client, $url, $filepath) {
return $client->getAsync($url, [
'sink' => $filepath
]);
};
}
};
$pool = new Pool($client, $requests(100));
$client=new\GuzzleHttp\client();
$requests=函数($total)使用($client){
对于($i=0;$i<$total;$i++){
$url=”http://domain.com/picture/{$i}.jpg”;
$filepath=“/tmp/{$i}.jpg”;
yield function()使用($client、$url、$filepath){
返回$client->getAsync($url[
“接收器”=>$filepath
]);
};
}
};
$pool=新池($client,$requests(100));
几乎是正确的,但是如果您想为池()提供“选项”
构造函数,它的实现是错误的
他错过了前面提到的池选项数组的关键实现
医生们说:
当迭代器生成函数时,将提供该函数
“request_options”数组,该数组应合并到任何
然后函数必须返回一个可等待的
答应我
另外,如果您查看我链接到的注释下面的Pool()
代码,您可以看到Guzzle的Pool调用了callable,并将Pool的“options”作为参数,这样您就可以将其应用于您的请求
正确的优先级是
按请求选项>池选项>客户端默认值
如果不将Pool()
对象的选项数组应用于请求对象,则会出现严重的错误,例如尝试创建新池($client,$requests(100),['options'=>['timeout'=>30.0]])代码>。如果没有我更正的代码,您的池选项将根本不会被应用,因为您不支持正确合并池选项,因此干脆放弃它们
下面是支持Pool()
选项的正确代码:
<?php
$client = new \GuzzleHttp\Client();
$requests = function ($total) use ($client) {
for ($i = 0; $i < $total; $i++) {
$url = "domain.com/picture/{$i}.jpg";
$filepath = "/tmp/{$i}.jpg";
yield function($poolOpts) use ($client, $url, $filepath) {
/** Apply options as follows:
* Client() defaults are given the lowest priority
* (they're used for any values you don't specify on
* the request or the pool). The Pool() "options"
* override the Client defaults. And the per-request
* options ($reqOpts) override everything (both the
* Pool and the Client defaults).
* In short: Per-Request > Pool Defaults > Client Defaults.
*/
$reqOpts = [
'sink' => $filepath
];
if (is_array($poolOpts) && count($poolOpts) > 0) {
$reqOpts = array_merge($poolOpts, $reqOpts); // req > pool
}
return $client->getAsync($url, $reqOpts);
};
}
};
$pool = new Pool($client, $requests(100));
它同时适用于所有请求。我需要为每个请求提供单独的$options,以便将每个响应正文下载到单独的文件中。如果将$options数组放在请求对象上,它将仅适用于该请求。。。我改写了我的句子问题是关于使用池,而不是单独的Request
objects;)无论如何,您必须创建请求对象。。。把它们递给游泳池。我在寻找我的问题的答案时发现了您的问题,请检查我如何创建一批请求并将其传递给batch方法:在这种情况下,如何返回正确的索引?假设您使用arrray$a['a']='url',$a['x']='url'等代替$total。如何知道哪个请求结果属于$a的哪个数组键?
// Using a closure that will return a promise once the pool calls the closure.
$client = new Client();
$requests = function ($total) use ($client) {
$uri = '127.0.0.1:8126/guzzle-server/perf';
for ($i = 0; $i < $total; $i++) {
yield function() use ($client, $uri) {
return $client->getAsync($uri);
};
}
};
$pool = new Pool($client, $requests(100));