Skip to content

Instantly share code, notes, and snippets.

@tuupola
Last active November 7, 2016 04:20
Show Gist options
  • Save tuupola/9e9061563e0b318c8a1f4e0519842d46 to your computer and use it in GitHub Desktop.
Save tuupola/9e9061563e0b318c8a1f4e0519842d46 to your computer and use it in GitHub Desktop.
<?php
require __DIR__ . "/vendor/autoload.php";
use Http\Client\Curl\Client;
use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Discovery\HttpClientDiscovery;
use Psr\Http\Message\ResponseInterface;
$client = new Client(new DiactorosMessageFactory(), new DiactorosStreamFactory());
$factory = new DiactorosMessageFactory();
$foo = $factory->createRequest("GET", "http://localhost:8081/foo");
$bar = $factory->createRequest("GET", "http://localhost:8081/bar");
$pop = $factory->createRequest("GET", "http://localhost:8081/pop");
$promises[] = $client
->sendAsyncRequest($foo)
->then(function (ResponseInterface $response) {
print date("H:s") . " got foo\n";
return $response;
});
$promises[] = $client
->sendAsyncRequest($bar)
->then(function (ResponseInterface $response) {
print date("H:s") . " got bar\n";
return $response;
});
$promises[] = $client
->sendAsyncRequest($pop)
->then(function (ResponseInterface $response) {
print date("H:s") . " got pop\n";
return $response;
});
try {
array_walk($promises, function($promise) {
$promise->wait();
});
} catch (\Exception $exception) {
print $exception->getMessage() . "\n\n";
}
<?php
require __DIR__ . "/vendor/autoload.php";
$app = new \Slim\App;
$app->get("/foo", function($request, $response, $args) {
sleep(3);
print "foo";
});
$app->get("/bar", function($request, $response, $args) {
sleep(3);
print "bar";
});
$app->get("/pop", function($request, $response, $args) {
sleep(3);
print "pop";
});
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment