cmdrsharp/guzzle-api

A cleaner approach to using Guzzle in Laravel

2.2.0.2 2020-11-17 08:06 UTC

This package is auto-updated.

Last update: 2024-04-17 15:38:17 UTC


README

Latest Stable Version Build Status Scrutinizer Code Quality StyleCI MIT licensed

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

Dylan DPC CmdrSharp

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.

License

The MIT License (MIT)