A PSR-18 adapter for Guzzle 6 client.
PHP 7.0 or above.
composer require mjelamanov/psr18-guzzle
require_once __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Request;
use Mjelamanov\GuzzlePsr18\Client;
$yourOptionalConfig = [];
$guzzleClient = new GuzzleClient($yourOptionalConfig);
$client = new Client($guzzleClient); // create an adapter instance
$request = new Request('GET', 'http://example.com'); // A PSR-7 request instance
$response = $client->sendRequest($request); // Sending request
var_dump($response->getStatusCode(), (string) $response->getBody()); // Prints response
How PSR-18 clients should handle errors, see here.
This library tries to follow to the that recommendation but Guzzle's custom handlers may break this compatibility.
In this case of you can tell me through "new issue" or send me a "pull request".
By default Guzzle client throws exceptions for HTTP 400 and 500 errors and this adapter is caught them and return
response as is. You can disable this behavior by http_errors
option but the result will be the same.
...
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Client\RequestExceptionInterface;
...
try {
$response = $client->sendRequest($request);
} catch (NetworkExceptionInterface $e) {
// Network issues
} catch (RequestExceptionInterface $e) {
// When request is invalid
} catch (ClientExceptionInterface $e) {
// In all other cases
}
composer test
The MIT license. Read LICENSE file.