本文整理汇总了PHP中GuzzleHttp\ClientInterface::sendAll方法的典型用法代码示例。如果您正苦于以下问题:PHP ClientInterface::sendAll方法的具体用法?PHP ClientInterface::sendAll怎么用?PHP ClientInterface::sendAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\ClientInterface
的用法示例。
在下文中一共展示了ClientInterface::sendAll方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: runBatch
public function runBatch()
{
$this->_guzzle->sendAll($this->_batch, ['error' => function (ErrorEvent $event) {
$batchId = $event->getRequest()->getHeader('X-Batch-ID');
$result = $this->_results[$batchId];
if ($result instanceof ApiResult) {
$result->readJson($event->getResponse()->getBody());
}
}, 'complete' => function (CompleteEvent $event) {
$batchId = $event->getRequest()->getHeader('X-Batch-ID');
$result = $this->_results[$batchId];
if ($result instanceof ApiResult) {
$result->readJson($event->getResponse()->getBody());
}
}]);
$this->_batch = [];
$this->_results = [];
}
示例2: batch
/**
* Convenience method for sending multiple requests in parallel and
* retrieving a hash map of requests to response objects or
* RequestException objects.
*
* Note: This method keeps every request and response in memory, and as
* such is NOT recommended when sending a large number or an indeterminable
* number of requests in parallel.
*
* @param ClientInterface $client Client used to send the requests
* @param array|\Iterator $requests Requests to send in parallel
* @param array $options Passes through the options available in
* {@see GuzzleHttp\ClientInterface::sendAll()}
*
* @return \SplObjectStorage Requests are the key and each value is a
* {@see GuzzleHttp\Message\ResponseInterface} if the request succeeded
* or a {@see GuzzleHttp\Exception\RequestException} if it failed.
* @throws \InvalidArgumentException if the event format is incorrect.
*/
function batch(ClientInterface $client, $requests, array $options = array())
{
$hash = new \SplObjectStorage();
foreach ($requests as $request) {
$hash->attach($request);
}
// Merge the necessary complete and error events to the event listeners
// so that as each request succeeds or fails, it is added to the result
// hash.
$options = RequestEvents::convertEventArray($options, array('complete', 'error'), array('priority' => RequestEvents::EARLY, 'once' => true, 'fn' => function ($e) use($hash) {
/** @noinspection PhpUndefinedMethodInspection */
$hash[$e->getRequest()] = $e;
}));
// Send the requests in parallel and aggregate the results.
$client->sendAll($requests, $options);
// Update the received value for any of the intercepted requests.
foreach ($hash as $request) {
/** @var \GuzzleHttp\Event\ErrorEvent[] $hash */
if ($hash[$request] instanceof CompleteEvent) {
$hash[$request] = $hash[$request]->getResponse();
} elseif ($hash[$request] instanceof ErrorEvent) {
$hash[$request] = $hash[$request]->getException();
}
}
return $hash;
}
示例3: batch
/**
* Convenience method for sending multiple requests in parallel and retrieving
* a hash map of requests to response objects or RequestException objects.
*
* Note: This method keeps every request and response in memory, and as such is
* NOT recommended when sending a large number or an indeterminable number of
* requests in parallel.
*
* @param ClientInterface $client Client used to send the requests
* @param array|\Iterator $requests Requests to send in parallel
* @param array $options Passes through the options available in
* {@see GuzzleHttp\ClientInterface::sendAll()}
* @return \SplObjectStorage Requests are the key and each value is a
* {@see GuzzleHttp\Message\ResponseInterface} if the request succeeded or
* a {@see GuzzleHttp\Exception\RequestException} if it failed.
* @throws \InvalidArgumentException if the event format is incorrect.
*/
function batch(ClientInterface $client, $requests, array $options = [])
{
$hash = new \SplObjectStorage();
foreach ($requests as $request) {
$hash->attach($request);
}
$handler = ['priority' => RequestEvents::EARLY, 'once' => true, 'fn' => function ($e) use($hash) {
$hash[$e->getRequest()] = $e;
}];
// Merge the necessary complete and error events to the event listeners so
// that as each request succeeds or fails, it is added to the result hash.
foreach (['complete', 'error'] as $name) {
if (!isset($options[$name])) {
$options[$name] = $handler;
} elseif (is_callable($options[$name])) {
$options[$name] = [['fn' => $options[$name]], $handler];
} elseif (is_array($options[$name])) {
$options[$name][] = $handler;
} else {
throw new \InvalidArgumentException('Invalid event format');
}
}
// Send the requests in parallel and aggregate the results.
$client->sendAll($requests, $options);
// Update the received value for any of the intercepted requests.
foreach ($hash as $request) {
if ($hash[$request] instanceof CompleteEvent) {
$hash[$request] = $hash[$request]->getResponse();
} elseif ($hash[$request] instanceof ErrorEvent) {
$hash[$request] = $hash[$request]->getException();
}
}
return $hash;
}