Skip to content

Instantly share code, notes, and snippets.

@tuupola
Last active November 11, 2016 14:06
Show Gist options
  • Save tuupola/527823d9a37848a6b35621204ec7b121 to your computer and use it in GitHub Desktop.
Save tuupola/527823d9a37848a6b35621204ec7b121 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 Http\Client\Socket\Client as Socket;
use GuzzleHttp\Client as GuzzleClient;
use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;
use Psr\Http\Message\ResponseInterface;
$nosuch = (new DiactorosMessageFactory)->createRequest("GET", "http://google.com/nosuch");
$curl = new Curl(new DiactorosMessageFactory, new DiactorosStreamFactory);
$react = new React(new DiactorosMessageFactory);
$guzzle = new Guzzle(new GuzzleClient);
$socket = new Socket(new DiactorosMessageFactory);
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: " . $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";
}
print "Socket async not supported \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 --version
PHP 7.0.11 (cli) (built: Sep 23 2016 20:33:19) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.11, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans
$ php httplug.php
Guzzle async exception: Http\Client\Exception\HttpException
Guzzle sync exception: Http\Client\Exception\HttpException
CURL async status: 404
CURL promise state: fulfilled
CURL sync status: 404
React async status: 404
React promise state: fulfilled
React sync status: 404
Socket async not supported
Socket sync status: 404
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment