cmdrsharp / guzzle-api
A cleaner approach to using Guzzle in Laravel
Installs: 128
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:libr
Requires
- php: >=7.4
- guzzlehttp/guzzle: >=7.0.1
- illuminate/support: >=8.0
Requires (Dev)
- dms/phpunit-arraysubset-asserts: >=0.1.0
- phpunit/phpunit: 8.0.*
README
This is an API for GuzzleHTTP. It aims to make using Guzzle a bit more clean and extends reusability.
Requirements
- PHP 7.4 or newer
- Laravel 8.x or newer
Note that the above requirements will always reflect the latest release. Older releases may support older PHP and Laravel versions.
Installation
Via composer
$ composer require cmdrsharp/guzzle-api
Usage
Inject the contract into the class where you need the client:
/** * @var RequestInterface */ protected $client; /** * @param Client $client */ public function __construct(Client $client) { $this->client = $client; }
You can then use the client by first calling make, to set the base URI - and then populating the request. The client returns a normal PSR ResponseInterface. This means you interact with the response as you would with any Guzzle response.
$this->client = $this->client->make('https://httpbin.org/'); $this->client->to('get')->withBody([ 'foo' => 'bar' ])->withHeaders([ 'baz' => 'qux' ])->withOptions([ 'allow_redirects' => false ])->asJson()->get(); echo $response->getBody(); echo $response->getStatusCode();
Alternatively, you can include both the body, headers and options in a single call.
$response = $this->client->to('get')->with([ 'foo' => 'bar' ], [ 'baz' => 'qux' ], [ 'allow_redirects' => false ])->asFormParams()->get(); echo $response->getBody(); echo $response->getStatusCode();
The asJson()
method will send the data using json
key in the Guzzle request. (You can use asFormParams()
to send the request as form params).
Available methods / Example Usage
// GET $response = $this->client->to('brotli')->get(); // POST $response = $this->client->to('post')->withBody([ 'foo' => 'bar' ])->asJson()->post(); // PUT $response = $this->client->to('put')->withBody([ 'foo' => 'bar' ])->asJson()->put(); // PATCH $response = $this->client->to('patch')->withBody([ 'foo' => 'bar' ])->asJson()->patch(); // DELETE $response = $this->client->to('delete?id=1')->delete(); // CUSTOM HEADER $response = $this->client->to('get')->withHeaders([ 'Authorization' => 'Bearer fooBar' ])->asJson()->get(); // CUSTOM OPTIONS $response = $this->client->to('redirect/5')->withOptions([ 'allow_redirects' => [ 'max' => 5, 'protocols' => [ 'http', 'https' ] ] ])->get();
Debugging
Using debug(bool|resource)
before sending a request turns on Guzzle's debugger, more information about that here.
The debugger is turned off after every request, if you need to debug multiple requests sent sequentially you will need to turn on debugging for all of them.
Example
$logFile = './guzzle_client_debug_test.log'; $logFileResource = fopen($logFile, 'w+'); $this->client->debug($logFileResource)->to('post')->withBody([ 'foo' => 'random data' ])->asJson()->post(); fclose($logFileResource);
This writes Guzzle's debug information to guzzle_client_debug_test.log
.
Versioning
This package follows Explicit Versioning.
Authors
Credits
Inspired by Dylan DPC - the current 1.0.0.0-release is currently also pulled as 0.4.0 in his repo! It's merely re-committed here so that I can easily adapt it as needed in the future.