Skip to content

A Simple Guzzle Client for a Laravel application

License

Notifications You must be signed in to change notification settings

udhuong/guzzle-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is this?

MIT licensed Latest Stable Version

A Simple Guzzle Client for a Laravel application

Requirements

  • PHP 7.1 or higher
  • Laravel 5.5 or 5.6

Installation

Via composer

$ composer require udhuong/guzzle-client

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

$client = $this->client
    ->make('https://api.example.abc/')
    ->withParamDefault([
        'agent' => [
            'device'     => detectDevice()->deviceType(),
            'platform'   => detectDevice()->platform(),
            'browser'    => detectDevice()->browser(),
            'ip'         => request()->ip(),
            'meta_agent' => detectDevice()->getUserAgent(),
        ],
    
    ])
    ->withHeaders([
        'token_master' => config('api.token_master')
    ]);

$response = $client
    ->to('feed')
    ->asQuery()
    ->withBody([
        'foo' => 'bar'
    ])
    ->get();

echo $response->getBody();
echo json_decode($response, true);
echo $response->getStatusCode();

Alternatively, you can include both the body, headers and options in a single call.

$response = $client
    ->to('get')
    ->with(
        [//body
            'foo' => 'bar'
        ], 
        [//headers
            'baz' => 'qux'
        ], 
        [//options
            'allow_redirects' => false
        ]
    )
    ->asFormParams()
    ->get();

echo $response->getBody();
echo json_decode($response, true);
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

$client = $this->client->make('https://api.example.abc/');

// 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');

//Add param default
$response = $client->to('post')->withParamDefault([
    'agent' => [
        'device'     => detectDevice()->deviceType(),
        'platform'   => detectDevice()->platform(),
        'browser'    => detectDevice()->browser(),
        'ip'         => request()->ip(),
        'meta_agent' => detectDevice()->getUserAgent(),
    ]
])->get();

//To upload file
$params = [
    'file' => $request->file('file')
];
$response = $client
    ->to('feed/store')
    ->asMultipart()
    ->withBody($params)
    ->post();

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+');

$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 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

UDHuong with Dylan DPC

License

The MIT License (MIT)

Copyright (c) 2021 UDHuong

About

A Simple Guzzle Client for a Laravel application

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%