You can execute http requests in parallel using Guzzle's Pool:batch()
method. It allows you to set default options for requests using options
key in the third parameter.
But what if I need different options for different requests in the pool? I would like to execute GET requests using a pool and stream each response to a different file on disk. There is a sink
option for that. But how to apply different values of this option to requests?
Rastor's example is almost right, but it's incorrectly implemented if you want to provide "options" to the
Pool()
constructor.He's missing the critical implementation of the Pool options array mentioned here.
The Guzzle docs say:
Also, if you look at the
Pool()
code below the comment I linked to, you can see that Guzzle's Pool calls the callable and gives it the Pool's "options" as the argument, precisely so that you are supposed to apply it to your request.The correct precedence is
If you don't apply the
Pool()
object's options array to your request objects, you will end up with severe bugs such as if you try making anew Pool($client, $requests(100), ['options'=>['timeout'=>30.0]]);
. Without my corrected code, your Pool-options won't be applied at all, since you didn't support merging the pool options properly and therefore simply ended up discarding them.So here is the correct code with support for
Pool()
options:Note however that you don't have to support the
Pool()
options, if you know that you will never be adding any options to yournew Pool()
constructor. In that case, you can just look at the official Guzzle docs for an example.The official example looks as follows:
You can specify the
$options
you want on the requests individually. It would only apply to all requests if you pass it to the client. Here's the excerpt from Guzzle 6 doc:See http://guzzle.readthedocs.org/en/latest/request-options.html?highlight=default#headers
For guzzle 6