Skip to content

Instantly share code, notes, and snippets.

@volkan
Created January 11, 2016 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volkan/be522078c9b8d23e858b to your computer and use it in GitHub Desktop.
Save volkan/be522078c9b8d23e858b to your computer and use it in GitHub Desktop.
<?php
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
$urls = ['http://www.google.com', 'http://www.facebook.com']
$client = new Client([
'verify' => false,
]);
$allResults = [];
$requests = [];
foreach ($urls as $url) {
$requests[] = $client->createRequest('GET', $url, [
'verify' => false,
'headers' => ['User-Agent' => 'Mozilla/5.0'],
'events' => [
'complete' => function (CompleteEvent $e) use (&$allResults, $url){
$response = $e->getResponse();
$allResults[$url] = $response;
},
'error' => function (ErrorEvent $e) use (&$allResults, $url){
$exception = $e->getException();
$allResults[$url] = $exception;
}
]
]);
}
// Results is a GuzzleHttp\BatchResults object.
$results = Pool::batch($client, $requests);
/** @var \GuzzleHttp\Message\ResponseInterface $response */
if (isset($allResults[$url]) && ($response = $allResults[$url]) && ($response instanceof \GuzzleHttp\Message\ResponseInterface)) {
$content = $response->getBody();
} elseif ($response instanceof \GuzzleHttp\Exception\ConnectException) {
/** @var \GuzzleHttp\Exception\ConnectException $response */
$exceptionMessage = $response->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment