Skip to content

Instantly share code, notes, and snippets.

@inpay
Last active August 29, 2015 14:19
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 inpay/c068a021017c8fa994a0 to your computer and use it in GitHub Desktop.
Save inpay/c068a021017c8fa994a0 to your computer and use it in GitHub Desktop.
Asynchronous API calls using Guzzle
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Event\CompleteEvent;
$client = new Client();
$requests = array(
"url1",
"url2"
);
function raport() {
echo "Do something after finishing all downloads";
}
$pool = new Pool($client, $requests);
$promise = $pool->send($client, $requests, [
'complete' => function (CompleteEvent $event) {
echo 'Completed request to ' . $event->getRequest()->getUrl() . "\n";
},
'error' => function (ErrorEvent $event) {
echo 'Request failed: ' . $event->getRequest()->getUrl() . "<br/>";
//echo $event->getException();
}
]);
$promise->then(raport());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment