Send multiple requests simultaneously
Parameters
$requests
arrayrequired- Request data (array of
'url'
,'headers'
,'data'
,'options'
) as per WpOrgRequestsTransport::request() $options
arrayrequired- Global options, see WpOrgRequestsRequests::response() for documentation
Source
*
* @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per \WpOrg\Requests\Transport::request()
* @param array $options Global options, see \WpOrg\Requests\Requests::response() for documentation
* @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well)
*
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access.
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
*/
public function request_multiple($requests, $options) {
// If you're not requesting, we can't get any responses ¯\_(ツ)_/¯
if (empty($requests)) {
return [];
}
if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) {
throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests));
}
if (is_array($options) === false) {
throw InvalidArgument::create(2, '$options', 'array', gettype($options));
}
$responses = [];
$class = get_class($this);
foreach ($requests as $id => $request) {
try {
$handler = new $class();
$responses[$id] = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']);
$request['options']['hooks']->dispatch('transport.internal.parse_response', [&$responses[$id], $request]);
} catch (Exception $e) {
$responses[$id] = $e;
}
User Contributed Notes
You must log in before being able to contribute a note or feedback.