dpc/guzzle-client

Simple Guzzle Client for a Laravel application

v0.3.3 2017-10-28 20:23 UTC

README

MIT licensed Latest Stable Version

A Simple Guzzle Client for a Laravel application

Requirements

  • PHP 7.1 or higher
  • Laravel 5.4 or 5.5

Installation

Via composer

$ composer require dpc/guzzle-client

After installation, publish the vendor files by running:

php artisan vendor:publish --provider="Dpc\GuzzleClient\GuzzleClientServiceProvider"

This will create a guzzle.php in the config directory which will contain:

return [
    'base_uri' => '',
];

Usage

Inject the contract into the class where you need the client:

/**
 * @var RequestClientContract
 */
protected $client;

/**
 * @param RequestClientContract $client
 */
public function __construct(RequestClientContract $client)
{
    $this->client = $client;
}

You can then use the client by:

$this->client->send('POST', 'foo/bar', [
    'foo' => 'random data',
])->asJson()->json());

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).

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)->send('POST', 'foo/bar', [
    'foo' => 'random data',
])->asJson()->json());

fclose($logFileResource);

This writes Guzzle's debug information to guzzle_client_debug_test.log.

Versioning

This package follows semver. Features introduced & any breaking changes created in major releases are mentioned in releases.

Support

This package is created as a basic wrapper for Guzzle based on what I needed in a few projects. If you need any other features of Guzzle, you can create a issue here or send a PR to master branch.

If you need help or have any questions you can:

Authors

Dylan DPC

License

The MIT License (MIT)

Copyright (c) 2017 Dylan DPC