mjelamanov / psr18-guzzle
A PSR-18 adapter for guzzle 6 client
Installs: 147 235
Dependents: 5
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=7.0
- guzzlehttp/guzzle: ^6.0
- psr/http-client: ^1.0
Requires (Dev)
- phpunit/phpunit: >=6.5
Provides
README
A PSR-18 adapter for Guzzle 6 client.
Requirements
PHP 7.0 or above.
Installation
composer require mjelamanov/psr18-guzzle
Example
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
Error handling
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 }
Test
composer test
License
The MIT license. Read LICENSE file.