Last active
August 29, 2015 14:22
-
-
Save eugene-dounar/6c9c4a765c4f6ac7e2b8 to your computer and use it in GitHub Desktop.
Guzzle concurrent promise requests
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 | |
use GuzzleHttp\Promise as P; | |
require_once __DIR__.'/vendor/autoload.php'; | |
function createChain($client, $i) { | |
$delay = rand(0, 5); | |
echo "delay-$i-$delay\n"; | |
$first = $client->getAsync("http://httpbin.org/delay/$delay")->then(function($resp) use($i) { | |
echo "$i-1\n"; | |
return json_decode($resp->getBody())->origin; | |
}); | |
$second = $first->then(function($ip) use ($client) { | |
return $client->getAsync("http://httpbin.org/get?ip=$ip&second"); | |
})->then(function($resp) use ($i) { | |
echo "$i-2\n"; | |
return json_decode($resp->getBody())->args; | |
}); | |
$third = P\all([$first, $second])->then(function($args) use ($client) { | |
list($ip, $get2) = $args; | |
return $client->getAsync("http://httpbin.org/get?ip=$ip&third"); | |
})->then(function($resp) use ($i) { | |
echo "$i-3\n"; | |
return json_decode($resp->getBody())->args; | |
}); | |
return P\all([$first, $second, $third]); | |
} | |
$chains = []; | |
$client = new GuzzleHttp\Client(); | |
for ($i = 0; $i < 10; $i++) { | |
$chains[] = createChain($client, $i); | |
} | |
$r = P\all($chains)->wait(); | |
var_dump($r); | |
echo "end\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment