2017 © Pedro Peláez
 

libr guzzle-client

Simple Guzzle Client for a Laravel application

image

dpc/guzzle-client

Simple Guzzle Client for a Laravel application

  • Sunday, March 25, 2018
  • by DPC
  • Repository
  • 5 Watchers
  • 7 Stars
  • 383 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 11 Versions
  • 4 % Grown

The README.md

What is this?

MIT licensed Latest Stable Version, (*1)

A Simple Guzzle Client for a Laravel application, (*2)

Requirements

  • PHP 7.1 or higher
  • Laravel 5.5 or 5.6

Installation

Via composer, (*3)

$ composer require dpc/guzzle-client

Usage

Inject the contract into the class where you need the client:, (*4)

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

/**
 * @param RequestClientContract $client
 */
public function __construct(RequestClientContract $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., (*5)

$client = $this->client->make('https://httpbin.org/');

$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., (*6)

$client = $this->client->make('https://httpbin.org/');

$response = $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)., (*7)

Available methods / Example Usage

$client = $this->client->make('https://httpbin.org/');

// Get request
$response = $client->to('brotli')->get();

// Post request
$response = $client->to('post')->withBody([
    'foo' => 'bar'
])->asJson()->post();

// Put request
$response = $client->to('put')->withBody([
    'foo' => 'bar'
])->asJson()->put();

// Patch request
$response = $client->to('patch')->withBody([
    'foo' => 'bar'
])->asJson()->patch();

// Delete request
$response = $client->to('delete?id=1')->delete();


// Headers are easily added using the withHeaders method
$response = $client->to('get')->withHeaders([
    'Authorization' => 'Bearer fooBar'
])->asJson()->get();


// Custom options can be specified for the Guzzle instance
$response = $client->to('redirect/5')->withOptions([
    'allow_redirects' => [
        'max' => 5,
        'protocols' => [
            'http',
            'https'
        ]
    ]
])->get();

// You can also specify the request method as a string
$response = $client->to('post')->withBody([
    'foo' => 'bar'
])->asJson()->request('post');

Debugging

Using debug(bool|resource) before sending a request turns on Guzzle's debugger, more information about that here., (*8)

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., (*9)

Example, (*10)

$logFile = './guzzle_client_debug_test.log';
$logFileResource = fopen($logFile, 'w+');

$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., (*11)

Versioning

This package follows semver. Features introduced & any breaking changes created in major releases are mentioned in releases., (*12)

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., (*13)

If you need help or have any questions you can: * Create an issue here * Send a tweet to @DPC_22 * Email me at dylan.dpc@gmail.com * DM me on the larachat slack team (@Dylan DPC), (*14)

Authors

Dylan DPC, (*15)

License

The MIT License (MIT), (*16)

Copyright (c) 2017 Dylan DPC, (*17)

The Versions

25/03 2018

dev-master

9999999-dev

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

25/03 2018

v0.4.1

0.4.1.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

25/03 2018

dev-dummy-pr-branch

dev-dummy-pr-branch

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

24/03 2018

v0.4.0

0.4.0.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

10/02 2018

v0.3.4

0.3.4.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

28/10 2017

v0.3.3

0.3.3.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

18/10 2017

v0.3.2

0.3.2.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

01/10 2017

v0.3.1

0.3.1.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

05/09 2017

v0.3

0.3.0.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

30/08 2017

v0.2

0.2.0.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires

 

16/08 2017

v0.1

0.1.0.0

Simple Guzzle Client for a Laravel application

  Sources   Download

MIT

The Requires