Last active
August 29, 2015 14:19
-
-
Save inpay/c068a021017c8fa994a0 to your computer and use it in GitHub Desktop.
Asynchronous API calls using Guzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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