Skip to content

Instantly share code, notes, and snippets.

@tuupola
Last active November 11, 2016 12:10
Show Gist options
  • Save tuupola/9862b11cc8fb6f7131545fffea900ee5 to your computer and use it in GitHub Desktop.
Save tuupola/9862b11cc8fb6f7131545fffea900ee5 to your computer and use it in GitHub Desktop.
<?php
require __DIR__ . "/vendor/autoload.php";
use Http\Client\Curl\Client as Curl;
use Http\Adapter\React\Client as React;
use Http\Adapter\Guzzle6\Client as Guzzle;
use GuzzleHttp\Client as GuzzleClient;
use Http\Client\Socket\Client as Socket;
use Http\Client\Common\PluginClient;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;
use Psr\Http\Message\ResponseInterface;
$nosuch = (new DiactorosMessageFactory)->createRequest("GET", "http://google.com/nosuch");
$curlClient = new Curl(new DiactorosMessageFactory, new DiactorosStreamFactory);
$reactClient = new React(new DiactorosMessageFactory);
$guzzleClient = new Guzzle(new GuzzleClient);
$socketClient = new Socket(new DiactorosMessageFactory);
$curl = new PluginClient($curlClient, [new ErrorPlugin]);
$react = new PluginClient($reactClient, [new ErrorPlugin]);
$guzzle = new PluginClient($guzzleClient, [new ErrorPlugin]);
$socket = new PluginClient($socketClient, [new ErrorPlugin]);
unset($promise);
$promise = $guzzle
->sendAsyncRequest($nosuch)
->then(function (ResponseInterface $response) {
print "Guzzle async status: ". $response->getStatusCode() . "\n";
return $response;
});
try {
$promise->wait();
print "Guzzle promise state: " . $promise->getState() . "\n";
} catch (\Exception $exception) {
print "Guzzle async exception: " . get_class($exception) . "\n";
}
try {
$response = $guzzle->sendRequest($nosuch);
print "Guzzle sync status: " . print $response->getStatusCode() . "\n";
} catch (\Exception $exception) {
print "Guzzle sync exception: " . get_class($exception) . "\n";
}
unset($promise);
$promise = $curl
->sendAsyncRequest($nosuch)
->then(function (ResponseInterface $response) {
print "CURL async status: ". $response->getStatusCode() . "\n";
return $response;
});
try {
$promise->wait();
print "CURL promise state: " . $promise->getState() . "\n";
} catch (\Exception $exception) {
print "CURL async exception: " . get_class($exception) . "\n";
}
try {
$response = $curl->sendRequest($nosuch);
print "CURL sync status: " . $response->getStatusCode() . "\n";
} catch (\Exception $exception) {
print "CURL sync exception: " . get_class($exception) . "\n";
}
unset($promise);
$promise = $react
->sendAsyncRequest($nosuch)
->then(function (ResponseInterface $response) {
print "React async status: ". $response->getStatusCode() . "\n";
return $response;
});
try {
$promise->wait();
print "React promise state: " . $promise->getState() . "\n";
} catch (\Exception $exception) {
print "React async exception: " . get_class($exception) . "\n";
}
try {
$response = $react->sendRequest($nosuch);
print "React sync status: " . $response->getStatusCode() . "\n";
} catch (\Exception $exception) {
print "React sync exception: " . get_class($exception) . "\n";
}
unset($promise);
$promise = $socket
->sendAsyncRequest($nosuch)
->then(function (ResponseInterface $response) {
print "Socket async status: ". $response->getStatusCode() . "\n";
return $response;
});
try {
$promise->wait();
print "Socket promise state: " . $promise->getState() . "\n";
} catch (\Exception $exception) {
print "Socket async exception: " . get_class($exception) . "\n";
}
try {
$response = $socket->sendRequest($nosuch);
print "Socket sync status: " . $response->getStatusCode() . "\n";
} catch (\Exception $exception) {
print "Socket sync exception: " . get_class($exception) . "\n";
}
/*
$ php plugin.php
Guzzle async exception: Http\Client\Exception\HttpException
Guzzle sync exception: Http\Client\Exception\HttpException
CURL async exception: Http\Client\Exception\RequestException
CURL sync exception: Http\Client\Common\Exception\ClientErrorException
React async status: 404
React promise state: fulfilled
React sync exception: Http\Client\Common\Exception\ClientErrorException
Socket async exception: Http\Client\Common\Exception\ClientErrorException
Socket sync exception: Http\Client\Common\Exception\ClientErrorException
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment