Skip to content

mjelamanov/psr18-guzzle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PSR-18 adapter for Guzzle 6

Build Status StyleCI Latest Stable Version Total Downloads License

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.

About

A PSR-18 adapter for guzzle 6 client

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages